Angular2 RC6禁用输入

前端之家收集整理的这篇文章主要介绍了Angular2 RC6禁用输入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
以前在我的Angular2 RC5应用程序中,我有一个输入元素,如下所示:
<input type="text" formControlName="blah" disabled/>

目的是使用户在编辑模式时不能编辑该字段;因此是禁用属性.

升级到Angular2 RC6后,我在控制台中收到以下消息:

It looks like you’re using the disabled attribute with a reactive form directive. If you set disabled to true when you set up this control in your component class,the disabled attribute will actually be set in the DOM for you. We recommend using this approach to avoid ‘changed after checked’ errors.

Example:

06001

但是,如果我遵循此建议,删除我的禁用属性,并将我的FormControl替换为禁用设置为true,那么该字段不会在提交时发布(即它不会出现在form.value中).

我是否错误地编码了这种情况? FormControl是否有一种方法被禁用以包含在表单值中?

作为一个附注,我实际上使用FormBuilder反对设置每个单独的FormControl,如果这有所作为.

正确的答案如Angular 2.4.1和使用FormBuilder像你一样
form: FormGroup;

constructor(private fb: FormBuilder) {

}

ngOnInit() {
    this.form = this.fb.group({
      blah: [{ value: '',disabled: true }]
});

你可以打电话来打开它

.this.form.get( “等等”)启用();

或通过电话关闭

.this.form.get( “等等”)禁用();

但是,浏览器不应该允许提交被禁用的元素.这个流行的问题有关于values of disabled inputs will not be submited?的更多信息

有各种各样的黑客和解决方法,人们已经提出了避免这种情况,例如隐藏的输入字段,readonly属性,或在提交之前启用字段.

原文链接:https://www.f2er.com/angularjs/142579.html

猜你在找的Angularjs相关文章