ngModel未在* ngFor Angular 4中更新

时间:2018-03-10 12:04:38

标签: angular angular2-ngmodel

当我通过单击“添加”按钮创建输入框时,输入框的

ngModel未更新。输入框正在填充。但是当我更改值时,相应的框值会在输入框中更新,但不会更新内插值(组件) 这是视图代码:

<div class=" dailyrep-div">  
    {{ settings_notification.thirtydreport_monthly.thirtydreport_monthly_time[i] | json }}
    <div class="d-inline-block inputwi140 marg-right15 marg-bottom15" *ngFor="let t of settings_notification.thirtydreport_monthly.thirtydreport_monthly_time; let i=index ; trackBy:trackByIndex " >
        <div class="input-group  marg-right15">
            <div class="input-group-prepend">
                <span class="input-group-text">
                    <i class="la la-clock-o"></i>
                </span>
            </div>
            <input class="form-control m-input m_timepicker" readonly  type="text" value="{{t.timeValue}}" [(ngModel)] = "settings_notification.thirtydreport_monthly.thirtydreport_monthly_time[i]"  > 
            <button *ngIf=" i != 0 " class="align-top btn btn-danger m-btn m-btn--icon m-btn--icon-only" (click)="delete_thirtyDays_Report_monthly_index(i)" ><i class="la la-remove"></i></button>
        </div>     
    </div>     
    <button *ngIf="settings_notification.thirtydreport_monthly.thirtydreport_monthly_time.length <=5 " class="align-top addbtn btn btn-success" (click)="add_thirtyDays_Report_monthly()"  ><i class="la la-plus"></i>
    Add</button>
</div>

这是我的组件代码:

trackByIndex(index: number, value: number) {
    return index;
  }   

  add_thirtyDays_Report_daily() {

    this.settings_notification.thirtydreport_daily.thirtydreport_daily_time.push({ timeValue: '12:42' });
    this.bindTimePickers();
}   

delete_thirtyDays_Report_daily_index(id) {
    console.log("delete option is: " + id);
    this.settings_notification.thirtydreport_daily.thirtydreport_daily_time.splice(id, 1);

}

1 个答案:

答案 0 :(得分:1)

您正在将值绑定到对象

this.settings.report.thirtydreport_monthly_time.push({ timeValue: '12:42' });

thirtydreport_monthly_time是一个带有对象{timeValue:&#39; time&#39; }

您需要使用html

中的相应模式进行绑定
[(ngModel)]="settings.report_monthly.thirtydreport_monthly_time[i]['timeValue']