等待Angular 2加载/解析FormBuilder/ControlGroups,然后渲染视图/模板 [英] Wait for Angular 2 to load/resolve FormBuilder/ControlGroups before rendering view/template
问题描述
有没有一种方法可以告诉Angular2等到FormBuilder
及其元素(及其关联的默认值)已加载,然后渲染模板?
Is there a way to tell Angular2 to wait until a FormBuilder
and its elements (with their associated default values) have loaded before rendering the template?
目前我的表单中有以下内容
Currently I have the following in my form
<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>
以及在我的构造函数中:
and in my constructor:
constructor(){
let fb = new FormBuilder();
this.FeeForm = fb.group({
ContentFee: ['0', Validators.required]
});
}
但是,在页面加载时,在我编辑费用之前,该值显示为NaN,理想情况下,我希望它在页面加载时显示其默认值(0.00).
However on the page load, before I edit the fee the value shows up as NaN, ideally I would like to have it show its default (0.00) on page load.
我该怎么做?
编辑
我注意到,如果用户[(ngModel)]不再提供FormBuilder的默认设置(当我尝试提交空白表单并记录FeeForm.value时,我未定义).周围有没有这个地方?
I notice that if user [(ngModel)] that the defaults of FormBuilder are no longer available (when I try submit a blank form and log the FeeForm.value i get undefined). Is there away around this?
推荐答案
好像您正在RC1中使用不推荐使用的表单,我知道这可以在RC2 +中的新表单中使用
Looks like you are using the deprecated forms in RC1, I know this works with the new forms in RC2+
如果您的组件中有一个Fee字段,则可以在构造函数中设置表单的初始值.
If you have a field Fee in your component you can set the initial value for the form in the constructor.
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
});
}
这篇关于等待Angular 2加载/解析FormBuilder/ControlGroups,然后渲染视图/模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!