I am using Angular 2 beta 5.
Here's my view:
<textarea [(ngModel)]="pendingMessage" (keydown.enter)="sendMessage()"></textarea>
And here's my component:
@Component({
//...
})
export class AppComponent {
private _pendingMessage: string;
public get pendingMessage() {
return this._pendingMessage;
}
public set pendingMessage(value: string) {
this._pendingMessage = value;
}
constructor() {
this.pendingMessage = "initial stuff"; //this shows up in the text field initially
}
public sendMessage() {
console.log(this.pendingMessage); //here, pending message is indeed whatever I typed in the text field
this.pendingMessage = "blah";
//here I expected the textfield to now contain "blah", but it doesn't
}
}
The whole concept of this two-way binding seems to be working alright. When I type some text in the field and hit enter, I can see that pendingMessage
is indeed whatever I typed into the field.
However, in the line below where I try to set the pendingMessage
to "blah"
, I was also expecting this to update the textfield to contain "blah". Why is this not the case? Isn't that the point of two-way bindings?
I know that Angular 2 can't magically know when values change, but I was hoping it would do a dirty check after evaluating the event. Is there any way to make it do such a thing automatically or somehow pick up the change? I would like to avoid using apply
.