Angular 2 ngmodel无法结合表达式


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

作为代码,我想将表达式带到ngmodel,但是报告错误,为什么?我该怎么做?

您可以使用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()">

您无法使用插值与模型绑定,您应该像(无{{}}(这样做:

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

最新更新