绑定到变量,绑定到函数以及TS角度的双向绑定

时间:2019-01-20 12:17:26

标签: angular typescript ionic-framework data-binding

我正在尝试绑定变量,方法和双向绑定。但是在所有3个上都会出错。下面是代码:

  • 绑定方法:
  

.html

<ion-label class="banner_small" [ngModel]="getUserName()"></ion-label>
  

.ts

getUserName() {
    return "test";
  }

错误是:

ERROR ReferenceError: $event is not defined
  • 绑定变量
  

.html

<ion-label class="banner_small" [ngModel]="userName"></ion-label>
  

.ts

export class MyApp {
  public userName: string;
constructor(...){
this.userName = "test";
...
}

错误是:

 Error: No value accessor for form control with unspecified name attribute
  • 双向绑定
  

.html

<ion-label class="banner_small" [(ngModel)]="getUserName()"></ion-label>
  

.ts

getUserName() {
    return "test";
  }

错误是:

 Uncaught Error: Template parse errors:Parser Error: Unexpected token '=' at column 14 in [getUserName()=$event]

1 个答案:

答案 0 :(得分:0)

第一个Mistkae,您尝试在标签上使用ngModel,应在输入上使用ngModel

如果您只想绑定到标签,只需使用 [innerHtml]

<ion-label class="banner_small" [innerHtml]="getUserName()"></ion-label>