我试图使用formControl中的disabled属性.当我将其放入模板中时,它会起作用:

<md-input formControlName="id" placeholder="ID" [disabled]="true"></md-input>

但浏览器提醒我:

看起来您正在将disabled属性与被动表单一起使用

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

所以我把它放进FormControl,然后从模板中删除:

constructor(private itemsService: ItemsService) {
    this._items = [];
    this.myForm = new FormGroup({
        id: new FormControl({value: '', disabled: true}, Validators.required),
        title: new FormControl(),
        description: new FormControl()
    });
    this.id = this.myForm.controls['id'];
    this.title = this.myForm.controls['title'];
    this.description = this.myForm.controls['description'];
    this.id.patchValue(this._items.length);
}

但它不起作用(它并没有禁用input).有什么问题吗?

推荐答案

我一直在使用[attr.disabled],因为我仍然喜欢这个模板驱动,而不是编程的enable()/disable(),因为它比IMO更优秀.

改变

<md-input formControlName="id" placeholder="ID" [disabled]="true"></md-input>

<md-input formControlName="id" placeholder="ID" [attr.disabled]="true"></md-input>

If you are on newer material change md-inputmat-input.

Typescript相关问答推荐

具有实体的ngrx SignalStore中的动态类型

如何使用TypScript限制允许对JavaScript值进行的操作集?

类型脚本不会推断键的值类型

typescribe不能使用值来索引对象类型,该对象类型满足具有该值的另一个类型'

为什么typescript不能推断类的方法返回类型为泛型''

为什么从数组中删除一个值会删除打字错误?

如何将类型从变量参数转换为返回中的不同形状?

在MessageStore对象中实现条件类型时出现TypeScrip错误

无法正确推断嵌套泛型?

有条件地删除区分的联合类型中的属性的可选属性

如何使用类型谓词将类型限制为不可赋值的类型?

避免混淆两种不同的ID类型

在正常函数内部调用异步函数

如何将通用接口的键正确设置为接口的键

如何创建由一个帐户签名但使用另一个帐户支付的Hedera交易

Route.ts文件中未导出HTTP方法

如何使用useSearchParams保持状态

如何在TypeScript中声明逆变函数成员?

在类型{}上找不到带有string类型参数的索引签名 - TypeScript

Angular另一个组件的nativeelement未定义