2

这是我的代码:

HTML:

<div *ngFor="let reject of rejects, let i = index">
    <h2>{{reject.reason}}</h2>
    <input type="text" [(ngModel)]="reject.reason" [ngFormControl]="inputField">
    <rt-dropdown (selectedReason)="selectReason($event.value, i)"></rt-dropdown>
</div>

和我的主要内容:

class {
    inputField = new Control();


    constructor( private _broadcastService : BroadcastService) {
        this.inputField.valueChanges
           .debounceTime(300)
           .subscribe(term => this._dropdownComponent.query(term));
    }
}



selectReason(text, index) {
 this.rejects[index].reason = text;
}

RT-dropdown 仅在您单击原因时发送原因。

问题是,当您单击原因时,{{reject.reason}} 会发生变化,但输入字段不会发生变化。

好像页面没有更新,有人对此有经验吗?

谢谢

4

2 回答 2

1

我会尝试以下方法:

<div *ngFor="let reject of rejects, let i = index">
  (...)
  <input type="text" [(ngModel)]="rejects[i].reason" 
         [ngFormControl]="inputField">
</div>

编辑

我会为相应的控件使用控件数组:

let inputFieldControls = new ControlArray([]);

for(let i = 0 ; (...) ; i++){
  inputFieldControls.push(new Control('' , Validators.required));
}

并以这种方式使用它:

<div *ngFor="let reject of rejects, let i = index">
  (...)
  <input type="text" [(ngModel)]="rejects[i].reason" 
         [ngFormControl]="inputFieldControls[i]">
</div>
于 2016-06-11T18:48:50.363 回答
1
<div *ngFor="let reject of rejects let idx=index">
    <h2>{{reject.reason}}</h2>
    <input type="text" [(ngModel)]="rejects[idx].reason" [ngFormControl]="inputField">
    <rt-dropdown (selectedReason)="selectReason($event.value, i)"></rt-dropdown>
</div>

您还需要尽可能多inputField = new Control()的(可能是一个数组,inputFields因为您有条目rejects并在其中引用它们)

[ngFormControl]="inputFields[idx]"

否则,所有输入元素都引用同一个Control实例。

于 2016-06-11T18:52:21.140 回答