其分配给变量something.commentaire
,要采取$event
数据,并用它做什么。
(ngModelChange)="doSomething($event)"
上面,$event
变量是从textarea
发射的变化。
component.ts
doSomething($event) {
// Do something with $event, you could for example,
// convert it to an URL format
// Imaginery function: "My name is Jeff" => "my-name-is-jeff"
const uri = makeUri($event)
// Now you have to set it to the variable you want it to
// bind to, so the model gets updated with the new value
this.something.commentaire = uri
}
如果你不需要做数据任何东西,只是想将其绑定到一个变量,你可以用“香蕉一箱”的语法:
[(ngModel)]="something.commentaire"
请注意,您不能使用elvis operator
等待数据加载,否则将打破模型绑定
[(ngModel)]="something?.commentaire" // Not working!
引擎盖下,这句法实际上取代:
[ngModel]="something.commentaire" (ngModelChange)="something.commentaire = $event"
我认为这可能是有用的为你检查出模板语法:) https://angular.io/docs/ts/latest/guide /template-syntax.html – Alex
@echonax我认为这个和你所指的稍有不同。 OP不知道'ngModelChange'通过'$ event'变量发出变化。 – borislemke
@borislemke即使那是真的,OP会通过查看链接获得这方面的知识:-) – echonax