Angular 从另一个方法指定ng模型时触发文本框的输入事件

Angular 从另一个方法指定ng模型时触发文本框的输入事件,angular,Angular,当从另一种方法分配分配ng模型值时,可以触发文本框的输入事件 <p><input type="text" [(ngModel)]="testValue" (input)="modelupdated(testValue)"/></p> <p><button type="button" class="btn btn-primary" (click)="openModal(template)"> Create template modal

当从另一种方法分配分配ng模型值时,可以触发文本框的输入事件

<p><input
type="text"
[(ngModel)]="testValue"
(input)="modelupdated(testValue)"/></p>


<p><button type="button" class="btn btn-primary" (click)="openModal(template)">
Create template modal</button></p>

创建模板模式

打字稿:

openModal(template: TemplateRef<any>) {
   this.testValue ="test";
    this.modalRef = this.modalService.show(template, Object.assign({}, { class: 'modal-sm' }));
}
OpenModel(模板:TemplateRef){
this.testValue=“test”;
this.modalRef=this.modalService.show(模板,Object.assign({},{class:'modalSM'}));
}
更改事件:

model更新(testValue){
log('from change');
log(testValue);
}


我在寻找的是,当模式打开时或API响应后,是否可以触发输入文本字段的输入/更改方法?

我建议采用一种反应式方法

  form: FormGroup;
  constructor(private formbuilder: FormBuilder) {}

  ngOnInit() {
    this.form = this.formbuilder.group({
      text: ["abc"]
    });
    this.form.controls['text'].valueChanges.subscribe(data=>{
      console.log('valuechanged' ,data);
    })
  }
  modelupdated(event) {
    console.log(this.text);
  }

  buttonClicked() {
    this.form.patchValue({
      text:'123'
    }) 
  }
HTML



我建议采用一种被动形式来实现this@boredbear153这个解决方案对你有帮助吗?
<form [formGroup]="form">
    <input
type="text"
formControlName="text"
/>
</form>