![](/img/trans.png)
[英]How to conditionally set a [disabled] attribute value in an Angular Reactive Form?
[英]How to make a disabled reactive form Editable in Angular2
我正在使用下面的代碼創建表單並將其設置為只讀
createForm() {
this.comapnyIdentificationForm = this.fb.group({
businessName: ['', Validators.required ],
adressPrimary: '',
adressSecondary: '',
city:'',
state: '',
zipCode: '',
country: '',
companyPhone: '',
DUNS: ''
});
this.comapnyIdentificationForm.disable();
}
我需要啟用它,並將編輯后的數據發布回Json:
<button type="button" (click)="EditData()" class="btn modal-btn btn-default">Edit</button>
只需使用以下代碼即可啟用您的表單。
this.comapnyIdentificationForm.enable();
獲取一個json對象。 使用以下代碼:
this.comapnyIdentificationForm.value;
要使用后端數據填寫表單,請使用以下代碼:this.comapnyIdentificationForm.patchValue(jsonData);
您也可以使用指令
@Directive({
selector: '[enableControl]'
})
export class EnableControlDirective {
@Input() set enableControl( condition : boolean ) {
if (condition)
this.ngControl.control.enable();
else
this.ngControl.control.disable();
}
constructor(private ngControl : NgControl ) {}
}
//use
<input class="form-control " [enableControl]="condition">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.