为“角度链"下拉列表设置默认值 [英] Set a default value for Angular chain dropdown

查看:71
本文介绍了为“角度链"下拉列表设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ID中有JSON国家/地区,我想要的是将defeulat值设置为selected,到目前为止,这里是我的选择代码:

I had JSON country within ID, and what I want to is the defeulat value is set to selected, here my select code so far:

<select (change)="onNationalitySelected($event.target.value)" id="nationality" formControlName="nationality">
                <option value=""> {{'messages.dropdownselect' | translate}} </option>
                <option *ngFor="let country of countries" [value]="country.id" [selected]="country.id == '100'" >
                  {{ country.name }}
                </option>
              </select>

.ts文件

  onNationalitySelected(nationalityId: number) {
    this.customer.coid = nationalityId;
    this.customer.nationality = this.countries.filter(
      c => (c.id == nationalityId)
    )[0].name;

我尝试将国家/地区的默认值设置为100,但是它不起作用,保存到数据库后该值仍然为空.

I tried to set a default value for country is 100, but it's not working, the value still empty after saved to DB.

感谢您的任何评论.

推荐答案

ts

// Let's use FormGroup so we can add a control/default
countryForm: FormGroup;

// define our countries data
countries = [{
 id: '100',
 name: 'USA',
 code: 'USD'
},
{
 id: '200',
 name: 'Canada',
 code: 'CAD'
},
{
 id: '300',
 name: 'UK',
 code: 'GBP'
}];

constructor(private fb: FormBuilder) {}

ngOnInit() {
 // set country code 100 as default
 this.countryForm = this.fb.group({
  countryControl: [this.countries[0].id]
 });
}

html

// ngValue allows us to use an object for default value assignment
<option [ngValue]="country.id" *ngFor="let country of countries">{{country.name}}</option>

我对countryControl: [this.countries[0].id]不确定,如果不起作用,请尝试以下方法:countryControl: [this.countries[0]]

I'm a little uncertain about countryControl: [this.countries[0].id], if it doesn't work, try this instead: countryControl: [this.countries[0]]

让我知道怎么回事!

这篇关于为“角度链"下拉列表设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆