首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Angular2 Reactive forms -使用下拉菜单设置表单域的默认值

Angular2 Reactive forms -使用下拉菜单设置表单域的默认值
EN

Stack Overflow用户
提问于 2016-09-29 17:08:01
回答 2查看 97.7K关注 0票数 32

如何为angular 2 reactive forms中的所有表单字段设置默认值?

以下是重现该问题的plnkr

下面的代码不会更新DropDrop值,因为它有一个关联的对象。

注意:这里我需要用从后端API接收到的响应来设置所有表单字段的默认值。

Component.html

代码语言:javascript
复制
<form [formGroup]="myForm" novalidate (ngSubmit)="save(myForm.value, myForm.valid)">
    <div class="form-group">
        <label>Country:</label>
        <select formControlName="country">
          <option *ngFor="let country of masterCountries" [ngValue]="country">{{country.countryName}}</option>
        </select>
    </div>

    <div class="form-group">
      <label for="">Name</label>
      <input type="text" class="form-control" formControlName="name">
      <small [hidden]="myForm.controls.name.valid || (myForm.controls.name.pristine && !submitted)" class="text-danger">
            Name is required (minimum 5 characters).
          </small>
      <!--<pre class="margin-20">{{ myForm.controls.name.errors | json }}</pre>-->
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
    <div class="margin-20">
      <div>myForm details:-</div>
      <pre>Is myForm valid?: <br>{{myForm.valid | json}}</pre>
      <pre>Is myForm submitted?: <br>{{submitted | json}}</pre>
      <pre>myForm value: <br>{{myForm.value | json}}</pre>
    </div>

Component.ts

代码语言:javascript
复制
export class AppComponent implements OnInit {
    public myForm: FormGroup;
    public masterCountries: any[] = [{"countryCode":"AF","countryName":"Afghanistan"},{"countryCode":"AL","countryName":"Albania"},{"countryCode":"DZ","countryName":"Algeria"},{"countryCode":"AS","countryName":"American Samoa"},{"countryCode":"AD","countryName":"Andorra"}];

    // Sample response received from backend api
    public CountryResponse = {country: {"countryCode":"AF","countryName":"Afghanistan"}, name: 'test123'};
    constructor(private _fb: FormBuilder) { }

    ngOnInit() {

        // the short way
        this.myForm = this._fb.group({
            country: [''],
            name: ['', [<any>Validators.required, <any>Validators.minLength(5)]],
        });


        (<FormGroup>this.myForm)
            .setValue(this.CountryResponse, {onlySelf: true});


    }


    save(model: User, isValid: boolean) {
        this.submitted = true;
        console.log(model, isValid);
    }
}

如果有其他方法来设置整个表单,请让我知道。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-29 18:34:56

只需更改以下内容:

代码语言:javascript
复制
 (<FormGroup>this.myForm)
            .setValue(this.CountryResponse, {onlySelf: true});

如下所示:

代码语言:javascript
复制
const selectedCountry = this.masterCountries.find(country => country.countryCode === this.CountryResponse.country.countryCode)
this.CountryResponse.country = selectedCountry;
(<FormGroup>this.myForm)
            .setValue(this.CountryResponse, {onlySelf: true});

如前所述,您需要传递与该对象完全相同的引用

票数 13
EN

Stack Overflow用户

发布于 2018-01-02 15:49:42

声明窗体控件或初始化时,将默认值作为参数传递

代码语言:javascript
复制
  MyFormControl: FormControl = new FormControl('Default value');
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39766029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档