Nar*_* CM 26 typescript angular2-forms angular
如何在角度2反应形式中为所有表单字段设置默认值?
这是重现问题的plnkr
下面的代码不会更新下拉列值,因为它有一个与之关联的对象.
注意:这里我需要使用从Backend API收到的响应为所有表单字段设置默认值.
Component.html
<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>
Run Code Online (Sandbox Code Playgroud)
Component.ts
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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果有任何其他方式来设置整个表单,请告诉我.
DeB*_*ges 21
我知道这是一个老问题,但如果有人找到它,现在有更好的方法在整个表单中设置值,使用PatchValue:
this.myForm.patchValue({
country: this.CountryResponse,
name: 'Any Name'
});
Run Code Online (Sandbox Code Playgroud)
这也允许部分,所以你仍然可以做类似的事情:
this.myForm.patchValue({ country: this.CountryResponse });
Run Code Online (Sandbox Code Playgroud)
或者您可以将值设置为单个控件:
this.myForm.get('country').setValue(this.CountryResponse);
Run Code Online (Sandbox Code Playgroud)
Har*_*inh 10
您的代码不起作用,因为尽管您的this.selectedCountry某个元素与其中一个元素具有相同的字段值this.masterCountries,但它们不是同一个对象.这使select.value === option.value比较总是返回false.
使其工作很简单,只需将您的setValue功能更改为:
(<FormControl>this.form.controls['country'])
.setValue(this.masterCountries[0], { onlySelf: true });
Run Code Online (Sandbox Code Playgroud)
或者您可以在创建时设置它FormGroup:
this.myForm = this._fb.group({
country: [this.masterCountries[0]],
name: ['', [<any>Validators.required, <any>Validators.minLength(5)]],
});
Run Code Online (Sandbox Code Playgroud)
Fab*_*nes 10
只需改变这个:
(<FormGroup>this.myForm)
.setValue(this.CountryResponse, {onlySelf: true});
Run Code Online (Sandbox Code Playgroud)
进入:
const selectedCountry = this.masterCountries.find(country => country.countryCode === this.CountryResponse.country.countryCode)
this.CountryResponse.country = selectedCountry;
(<FormGroup>this.myForm)
.setValue(this.CountryResponse, {onlySelf: true});
Run Code Online (Sandbox Code Playgroud)
如前所述,您需要传递完全相同的对象引用
当您声明表单控件或初始化时,将默认值作为参数传递
MyFormControl: FormControl = new FormControl('Default value');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54724 次 |
| 最近记录: |