簡體   English   中英

ng-select 禁用表單控件在反應形式角度不起作用

[英]ng-select disable form control not working in reactive form angular

我試圖為我的表單設置值並想禁用該表單。

不知何故,如果我設置值來形成它工作正常,但是一旦我編寫禁用方法,我的 ng-select 值就會消失。

有沒有人在禁用帶有值的 ng-select 時遇到同樣的問題?

 form = new FormGroup({ code: new FormControl() }); values = [{ value: "0" label: "test1" }, { value: "1" label: "test2" } ] //I am setting values using setvalue and disbling after wards this.form.controls['code'].setValue('0') this.form.disable();
 <ng-select formControlName="code" [items]="values" bindValue="value" labelForId="code" [clearable]="false" [selectOnTab]="true" placeholder="Select one" data-e2e-id="code"> </ng-select>

你確定你改變了價值?

設置值的方法:

使用 setValue() 的方法

this.form.get("code").setValue('0');
this.form.controls["code"].setValue('0');

使用 patchValue() 的方法

this.form.get("code").patchValue('0');
this.form.controls['code'].patchValue('0');
this.form.patchValue({"code": '0'});

在您的情況下可能是表單僅在應用 disablibg 后更新的問題,並且您沒有看到您以錯誤的方式更新提交

您可以在初始化時禁用/啟用表單控件,方法是將disabled設置為true ,如下所示

users.push(this.fb.group({
  userType: this.fb.control({value: 'Admin', disabled: true})
}));

您可以通過調用該特定表單控件的禁用/啟用方法來動態執行它。

// To enable form control
userType.enable();

// To disable form control
userType.disable();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM