加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular2 RC6禁用输入

发布时间:2020-12-17 07:37:18 所属栏目:安全 来源:网络整理
导读:以前在我的Angular2 RC5应用程序中,我有一个输入元素,如下所示: input type="text" formControlName="blah" disabled/ 目的是使用户在编辑模式时不能编辑该字段;因此是禁用属性. 升级到Angular2 RC6后,我在控制台中收到以下消息: It looks like you’re us
以前在我的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属性,或在提交之前启用字段.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读