Angular 4/2 using FormBuilder can I set place holder and label?

时间:2017-08-04 12:22:08

标签: angular angular-forms

I am developing application where all the labels for the controls are coming from a database and what I want is when I am binding form using Form Builder, to set label of that control at the same time, and use it as a place holder in that control.

Here is what I want:

<label class="control-label col-md-2">
  {from form builder property}
</label>
<div class="col-md-3">
  <div class="form-group">
    <input type="text" 
      formControlName="LName" 
      class="form-control" 
      placeholder="{from form builder property}" />
  </div>
</div>

Is there a way to do it in angular 4/2?

1 个答案:

答案 0 :(得分:1)

您无法为FormControl添加占位符/标签值,因此您无法在模板中访问它。

从您从服务器检索的对象中获取它:

<label class="control-label col-md-2">
  {{ data.fieldLabel }}
</label>
<div class="col-md-3">
  <div class="form-group">
    <input type="text" 
      formControlName="LName" 
      class="form-control" 
      [placeholder]="data.fieldPlaceholder" />
  </div>
</div>

或者另一种方法是使用nice ng-formly模块仅在没有编码模板的情况下从JSON对象构建表单。