首页 文章

角度反应形式禁用组

提问于
浏览
0

我正在使用Angular反应形式 . 在我的页面上,我需要选择启用/禁用字段(当前密码和新密码确认),以便我可以使用 form.value 并仅获取我需要的东西(禁用控件赢得't be in there). However, I'已找到禁用 FormControl 的方法但是,我需要禁用整个团体 .

那是我的小组

formGroup: FormGroup = this.fb.group({
...
        password: this.fb.group({
            current: ['', Validators.required],
            new: this.fb.group({
                password: [{
                    value: '',
                    disabled: this.changePassword.value
                }, Validators.required],
                passwordConfirm: [{
                    value: '',
                    disabled: this.changePassword.value
                }, Validators.required]
            }, {validator: PasswordValidator.MatchPassword})
        })
    });

这并没有真正起作用 . 如何通过将禁用状态附加到独立FormControl(复选框)来禁用整个 password FormGroup .

2 回答

  • 1

    你可以在 form 标签内打开 fieldset 标签,并根据表格控制值使用 disable 指令

    <fieldset [disabled]="changePassword">
    
  • 0

    解决了

    ngOnInit() {
            this.formGroup.get('password').enable();
            // changePassword: FormControl
            this.changePassword.valueChanges.subscribe(e => {
                if (!this.changePassword.value) {
                    this.formGroup.get('password').disable();
                } else {
                    this.formGroup.get('password').enable();
                }
            });
        }
    

相关问题