dyh333
dyh333

Reputation: 395

angular2+ ngModel can not binding an expression?

<input type="text" name="username" [(ngModel)]="{{_current + 1}}">

as the code, I want to banding an expression to ngModel, but report error, why? and how can I do it?

Upvotes: 2

Views: 156

Answers (2)

bluray
bluray

Reputation: 1953

You can use ngModelChange:

@Component(
 selector: 'my-component',
 templateUrl: './my-component.component.html',
)
export class MyComponent{
 _current: number;

 onChange(){
   this._current= this._current+ 1;
 }
}

my-component.component.html

<input type="text" name="username" [(ngModel)]="_current" (ngModelChange)="onChange()">

Upvotes: 3

Anshuman Jaiswal
Anshuman Jaiswal

Reputation: 5462

You can not bind with model using interpolation, you should do it like(without {{}}):

<input type="text" name="username" [(ngModel)]="_current">

Upvotes: 2

Related Questions