7

我有一个使用 Kendo Angular Grid 控件的 Angular 4.10 应用程序。我正在使用外部编辑。我创建了 FormGroup 如下:

        this.editForm = new FormGroup({
        'Id': new FormControl({ value: 0, disabled: true }),
        'Name': new FormControl("", Validators.compose([Validators.required, Validators.maxLength(30)])),
        'BlindName': new FormControl({ value: "" }, Validators.compose([Validators.required, Validators.maxLength(30)])),
        'UnitText': new FormControl(0),
        'IsFromBsp': new FormControl(true),
        'Disabled': new FormControl(false),
        'SortOrder': new FormControl(0, Validators.compose([Validators.required, Validators.pattern('\\d')]))
    });

我想做的是根据值 IsFromBsp 为字段 BlindName 设置禁用状态。就像是:

'BlindName': new FormControl({ value: "", disabled: this.IsFromBsp }, Validators.compose([Validators.required, Validators.maxLength(30)])),

有没有办法做到这一点?请告诉我。谢谢

4

2 回答 2

8

我假设您想禁用输入字段(如果IsFromBsp是)true。如果仅在最初需要,您可以在构建表单后运行一个函数:

check() {
  if(this.editForm.get('IsFromBsp').value == true) {
    this.editForm.get('BlindName').disable()
  }
}

如果此值发生更改,您必须在某些更改事件上再次调用此函数,或者使用(change)或然后使用valueChanges来监视表单值的更改,如果该值是其他内容,true则无法this.editForm.get('BlindName').enable()再次启用它。这适用于“常规”反应形式,希望也适用于剑道。

于 2017-05-31T17:21:27.250 回答
0

我不知道这个剑道是如何工作的,但在 html 中你可以这样做:

 <input type="text" [disabled]="form.controls['IsFromBsp']==='' && form.controls['IsFromBsp'].touched" formControlName="something"> 
于 2017-05-31T16:49:08.833 回答