简体   繁体   中英

Two-way binding for select Angular2

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM