禁用FormControl不能与逻辑一起使用

禁用FormControl不能与逻辑一起使用

本文介绍了禁用FormControl不能与逻辑一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从ts文件中禁用一个字段.当我true/false静态时,它工作正常.但我想在逻辑上做到这一点.当我的表单处于编辑模式时,我将使字段可编辑.当表单处于查看模式时将禁用这些字段.

I want to make a field disabled from ts file.it is working fine when I true/false statically. but I want to make it logically. when my form in edit mode I will make the fields editable. when the form in view mode will disable the fields.

  Example:
  form = new FormGroup({
    first: new FormControl({value: 'Nancy', disabled: true}, Validators.required),
    last: new FormControl('Drew', Validators.required)
  });

更改模式时的逻辑

editProspectDetails() {
    this.editProspectMode = !this.editProspectMode;
    this.isProspectDisabled();
}

FormControl名称:

this.fb.group({
prospect_pref_name: new FormControl({value: '', disabled: this.isProspectDisabled()}),
})

isProspectDisabled(){
    return true; // working

    but i want to make it conditionally
    console.log(this.editProspectMode); // it will return : true/ false
    return this.editProspectMode;
}

推荐答案

您可以简单地使用disable()禁用字段

You can simply use disable() to disable field

尝试

this.fb.group({
prospect_pref_name: new FormControl(''),
}

和您的editProspectDetails()

editProspectDetails() {
    this.editProspectMode = !this.editProspectMode;
    this.your_form_name.controls.prospect_pref_name.disable();
}

这篇关于禁用FormControl不能与逻辑一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:29