0

I'm trying to take text from one of my component.html files and save the input to a variable in a method in one of my .ts files. Can you help?

home.component.html file:

Username: <input ng-model="username"><br>
Password: <input ng-model="pass">  
<button (click)="login()">Login</button>

home.component.ts file:

export class HomeComponent implements OnInit {

    login()
    {
     const usr= {{username}};
     const password={{pass}};
     print("user is"+usr " Password is"+password);

    }

}

2 Answers 2

1

If you are using angular it should be

ngModel instead of ng-model

Username: <input  [(ngModel)]="username"><br>
Password: <input  [(ngModel)]="pass">  

and in component.ts

export class HomeComponent implements OnInit {
    username : string;
    password : string;

    login()
    {
     const usr= this.username;
     const password= this.password;
     console.log("user is"+usr " Password is"+password);
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. how do I return the values of "usr" and "password" back to the html instead of writing to console?
why do you want to retun? its already there in HTML. you can use it as {{user}} {{password}}
I'm actually returning different text, I just want to understand how to
0

you need to change ng-model to ngModel

ng-model :- angularjs

ngModel :- Angular 2 or greater

Html

Username: <input [(ngModel)]="username"><br>
Password: <input [(ngModel)]="pass">  
<button (click)="login()">Login</button>

Component

export class HomeComponent implements OnInit {
 public username: String;
 public pass: String;
 public function login() {
 console.log('User Name: ' + this.username);
 console.log('Password: ' + this.pass);
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.