简体   繁体   English

Angular 2和选择与Formbuilder一起使用的占位符

[英]Angular 2 and select placeholder working with formbuilder

I have problem with placeholder in my drop-down list. 我的下拉列表中的占位符有问题。 When I'm using only html code, it works correct. 当我只使用html代码时,它可以正常工作。 HTML code: HTML代码:

<select  
    class="form-control"
    #selectedItem
    required
    formControlName="town"
>
    <option [ngValue]="undefined" value="undefined" disabled selected>Wybierz miasto</option>
    <option class="city-option">Warszawa</option>
    <option class="city-option">Kraków</option>
    <option class="city-option">Wrocław</option>
</select>

The problem is, when I'm trying to put it into formbuilder, the placeholder is disapear. 问题是,当我尝试将其放入formbuilder时,占位符消失了。 Part of component.ts code component.ts代码的一部分

ngOnInit() { 
 this.searchForm = this._formBuilder.group({
  'town': ['tmp']
  });
}

How to make it work? 如何使其运作?

The value in your FormGroup needs to be the same as the value of the option you want to be selected by default: FormGroup中的值必须与您希望默认选择的选项的值相同:

ngOnInit() { 
  this.searchForm = this._formBuilder.group({
    'town': null
});
<option [ngValue]="null" disabled selected>Wybierz miasto</option>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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