Reputation: 23
This is html
<input type="text" [(ngModel)]="name" />
<button (click)="addtext()">add</button>
this is ts
import { Component, OnInit } from "@angular/core";
@Component({
selector: "app-post",
templateUrl: "./post.component.html",
styleUrls: ["./post.component.css"],
})
export class PostComponent implements OnInit {
constructor() {}
ngOnInit(): void {}
name: string = "";
addtext() {
console.log("your name is: ", name);
}
}
please help me binding this thing,something wrong
i want to write in log the text for name and bind ngmodel as text ,name
Upvotes: 0
Views: 92
Reputation: 654
Name is a member variable of the PostComponent class. To access the name instance variable within any instance methods (in this case addtext), you need to reference it using the this
keyword. Otherwise, it tries to access a local variable called name inside the scope of the addtext function and since there is no variable called name in that scope, it will print undefined
. Hope this helps.
addtext()
{
console.log("your name is: " + this.name);
}
Upvotes: 1