我不是Angular事件的专家,但这是我的问题。如果我运行以下代码,则会用错误的信息大声地更新后端,因为item.their_platform
在togglePlatform()
触发之前它不会发生变化。
模板:
<mat-checkbox (ngModelChange)="togglePlatform()" [(ngModel)]="item.their_platform"></mat-checkbox>
TS:
togglePlatform(){
//update backend with the new value for item.their_platform
}
我已经通过在togglePlatform
方法中使用超时解决了这个问题,尽管我希望在这种情况下我可以将其他方法绑定到该方法上,这才有意义。
有更好的活动吗?这是使用mat-checkbox与使用Vanilla Angular的副作用吗?
谢谢
不要总是依赖执行的顺序,因为您不一定总是之前到达的。在您的情况下,解决方案应为:
<mat-checkbox (ngModelChange)="togglePlatform($event)" [(ngModel)]="item.their_platform"></mat-checkbox>
togglePlatform(value) {
// update backend with the value, not item.their_platform
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句