我需要更新js更改事件上的角模型,这是一个简化的、孤立的演示:
英雄-form.分量.hero:
<button type="button" id='btn1'>change</button>
<input type="text" id="txt1" name="txt1" [(ngModel)]="str1" />{{str1}}英雄-表单.组件.:
...
import * as $ from "jquery";
...
export class HeroFormComponent implements OnInit {
str1 = "initval";
ngAfterViewInit(){
var txt1 = $('#txt1');
$('#btn1').on('click', function(){
txt1.val('new val').change();
// when js/jquery triggers a change on the input, I need the str1
// which was bound using [(ngModel)] to be updated
});
}单击该按钮时,文本框的值将更改为new val,但插值{{str1}}不会受到影响,但是如果手动更改textbox的值,则效果良好。
是否可以在js更改事件上更新使用ngmodel绑定的模型?
发布于 2019-04-29 11:58:51
在角度项目中,我们不应该像您的方式那样实现您的需求。
您可以使用(click)事件和#txt1获得值。
在ts中,组件实现更改str1值。
export class AppComponent {
name = 'Binding data in Angular';
str1 = "initval";
ngAfterViewInit() {
}
change(val) {
console.log(val)
this.str1 = val;
}
}更新的HTML
<hello name="{{ name }}"></hello>
<button type="button" id='btn1' (click)="change(txt1.value)">change</button>
<input type="text" #txt1 id="txt1" name="txt1" />{{str1}}https://stackoverflow.com/questions/55902762
复制相似问题