等待Angular 2在渲染视图/模板之前加载/解决FormBuilder/Control组



是否有一种方法告诉angular2等到 FormBuilder及其元素(带有关联的默认值) 在呈现模板之前已加载?

目前我的表格有以下内容

<form (ngSubmit)="submitFees()" [ngFormModel]="FeeForm">
    <div class="form-group row">
      <div class="col-sm-12">
        <input type="number" id="Fees" class="form-control"
        placeholder="Fee" required
       [ngFormControl]="FeeForm.controls['ContentFee']"
        [(ngModel)]="Fee">
      </div>
     Fee: {{postFee*1.2| number:'1.2-2'}}
  </div>
</form>

和我的构造函数:

constructor(){
    let fb = new FormBuilder();
    this.FeeForm = fb.group({
        ContentFee: ['0', Validators.required]
    });
}

但是,在页面加载上,在我编辑费用之前,值以NAN为单位,理想情况下,我想在页面加载上显示其默认值(0.00)。

我将如何完成此操作?

编辑

我注意到,如果用户[(ngmodel)]不再可用formbuilder的默认值(当我尝试提交空白表格并记录feeform.value时,我会得到未定义)。是否有?

看起来您正在使用rc1中的不弃用表格,我知道这与rc2

中的新表格一起起作用

如果您的组件中有字段费用,则可以为构造函数中的表单设置初始值。

constructor(){
    this.Fee = 0 // This will show up as the default value 
    let fb = new FormBuilder();
    this.FeeForm = fb.group({
        ContentFee: ['0', Validators.required] // Value not used
    });
}

最新更新