Angular 2 ngModel 无法绑定(bind)表达式?

标签 angular

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

作为代码,我想给ngModel绑定(bind)一个表达式,但是报错,为什么? 我该怎么做?

最佳答案

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

关于Angular 2 ngModel 无法绑定(bind)表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49548138/

相关文章:

javascript - 在 Apache 服务器上部署 Angular 2 应用程序

angular - 如何在我的 Angular 应用程序中共享 ng-template?

dynamic - Angular 2(最终): resetConfig to add routes to lazy loaded routes

angular - 如何在管道内调用 forkJoin?

angular - 创建集中的异常处理以从未处理的异常中恢复

angular - 当元素已经存在时,为什么 Testcafe 还要等待元素出现?

javascript - angular 2无法引用函数内的变量

在 RxJs 订阅后,来自服务的 Angular 2 返回数据不可用

angular - 类型错误 : Invalid Event Target - But just by using direct Route

Angular 2 Material 自动完成选定值