Praveen Rawat
Praveen Rawat

Reputation: 658

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

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?

Upvotes: 2

Views: 2806

Answers (1)

bertrandg
bertrandg

Reputation: 3207

You can't add placeholder/label value to a FormControl so you can't access thm in you template..

Just get it from the object you retrieve from the server:

<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>

Or an other way is to use the nice ng-formly module to build you forms only from JSON objects without coding templates.

Upvotes: 1

Related Questions