4

Angular2, in my ts, I have a control group, how can I have the two-way binding for the select in my html using ngFormControl?

form.component.ts

this._reportGeneratingForm = fb.group({
 ......

  selectedGroup: ['']
})

form.component.html

  <select class="form-control" ????>
      <option>Day</option>
      <option>Hour</option>
      <option>week</option>
      <option>Month</option>
    </select>

2 Answers 2

7
<select class="form-control" [(ngModel)]="someProperty">
  <option>Day</option>
  <option>Hour</option>
  <option>week</option>
  <option>Month</option>
</select>

where someProperty is a property on the components class that holds the value or

<select class="form-control" [ngFormControl]="selectControl">
  <option>Day</option>
  <option>Hour</option>
  <option>week</option>
  <option>Month</option>
</select>

This only works properly on all browsers if you have a recent Angular2 version (>= beta.16)

Sign up to request clarification or add additional context in comments.

6 Comments

can I use ngFormControl?
Yes, I tried ngFormControl. It is working. For my case, I may not need a data model
selected="selected" is not working, override by the data-binding
Tks Gunter, it is working now <select class="form-control" id="groupingBySelect" [ngFormControl]="_reportGeneratingForm.controls['grouping']"> <option selected="selected">Day</option> <option>Hour</option> <option>week</option> <option>Month</option> </select>
just to update that selected attribute is not working in this approach, you have to set the grouping value in ts
|
0

In this example selected atribute is not working ! But in ts file

  • For the ngModel : You can write as someProperty = Day
  • For the ngFormControl: You can also write as selectControl.value= Day

It will works fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.