禁用 angular 反应形式的字段
Disable a field in angular reactive form
在我的 angular 6 应用程序中,我正在制作一个反应式表格,其中有,
Html
<form [formGroup]="form">
<h2>Click the add button below</h2>
<button (click)="addCreds()">Add</button>
<div formArrayName="credentials" >
<div *ngFor="let creds of form.get('credentials').controls; let i = index"
[formGroupName]="i" style="display: flex">
<select formControlName="action">
<option *ngFor="let option of options" value="{{option.key}}">
{{option.value}}
</option>
</select>
<input placeholder="Name" formControlName="name">
<div *ngIf="creds.value.action=='set' ||
creds.value.action=='go'">
<input placeholder="Label" formControlName="label">
</div>
</div>
</div>
</form>
这里我使用了
<div *ngIf="creds.value.action=='set' || creds.value.action=='go'">
<input placeholder="Label" formControlName="label">
</div>
如果条件为真,将显示字段label
,否则将不显示。
但我只需要禁用该字段,不应将其完全删除..
我已经尝试过了,
<input [disabled]="creds.value.action != 'set' ||
creds.value.action != 'go' " placeholder="Label" formControlName="label">
但是不行。
Stackblitz 与 *ngIf https://stackblitz.com/edit/angular-form-array-example-25y1ix
禁用 Stackblitz https://stackblitz.com/edit/angular-form-array-example-laftgu
如果所选 action
(第一个下拉列表)值为 wait
..
,请帮助我如何禁用 label
字段
只需创建一个 CSS class:
.disabled {
pointer-events:none;
background-color: #D3D3D3;
}
向您的组件添加方法:
getValueByIndex(index) {
const actionValue = ( < FormArray > this.form.get('credentials')).at(index).value.action;
return actionValue !== 'set' && actionValue !== 'go';
}
在您的模板中使用此方法:
<input
[class.disabled]="getValueByIndex(i)"
placeholder="Label"
formControlName="label">
Here's a Working Sample StackBlitz for your ref.
更新:
或者,您可以执行以下操作:
默认禁用 label
字段:
addCreds() {
const creds = this.form.controls.credentials as FormArray;
creds.push(this.fb.group({
action: '',
name: '',
label: {
value: '',
disabled: true
},
state: 'na'
}));
}
然后监听表单上的(ngModelChange)
事件来更新标签字段的状态:
<select
formControlName="action"
(ngModelChange)="($event === 'set' || $event === 'go') ? creds.controls.label.enable() : creds.controls.label.disable()">
<option
*ngFor="let option of data"
[value]="option.key">
{{option.value}}
</option>
</select>
这里有一个 Working Sample StackBlitz 这种方法。
感谢 A.Winnen 对使用先前方法的性能影响的评论。
在 Reactive Form 中你不能使用 disabled 指令。相反,您需要通过调用 enable() 或 disable() 函数来禁用 formControl 'reactive way'。
addCreds() {
const creds = this.form.controls.credentials as FormArray;
const newGroup = this.fb.group({
action: '',
name: '',
label: ''
});
newGroup.controls.action.valueChanges.subscribe((currentAction) => {
if (typeof currentAction === "string" && currentAction.toLowerCase() === 'wait') {
newGroup.controls.label.disable();
} else {
newGroup.controls.label.enable();
}
});
creds.push(newGroup);
}
我修改了你的 stackblitz 示例:https://stackblitz.com/edit/angular-form-array-example-ymysw4
在我的 angular 6 应用程序中,我正在制作一个反应式表格,其中有,
Html
<form [formGroup]="form">
<h2>Click the add button below</h2>
<button (click)="addCreds()">Add</button>
<div formArrayName="credentials" >
<div *ngFor="let creds of form.get('credentials').controls; let i = index"
[formGroupName]="i" style="display: flex">
<select formControlName="action">
<option *ngFor="let option of options" value="{{option.key}}">
{{option.value}}
</option>
</select>
<input placeholder="Name" formControlName="name">
<div *ngIf="creds.value.action=='set' ||
creds.value.action=='go'">
<input placeholder="Label" formControlName="label">
</div>
</div>
</div>
</form>
这里我使用了
<div *ngIf="creds.value.action=='set' || creds.value.action=='go'">
<input placeholder="Label" formControlName="label">
</div>
如果条件为真,将显示字段label
,否则将不显示。
但我只需要禁用该字段,不应将其完全删除..
我已经尝试过了,
<input [disabled]="creds.value.action != 'set' ||
creds.value.action != 'go' " placeholder="Label" formControlName="label">
但是不行。
Stackblitz 与 *ngIf https://stackblitz.com/edit/angular-form-array-example-25y1ix
禁用 Stackblitz https://stackblitz.com/edit/angular-form-array-example-laftgu
如果所选 action
(第一个下拉列表)值为 wait
..
label
字段
只需创建一个 CSS class:
.disabled {
pointer-events:none;
background-color: #D3D3D3;
}
向您的组件添加方法:
getValueByIndex(index) {
const actionValue = ( < FormArray > this.form.get('credentials')).at(index).value.action;
return actionValue !== 'set' && actionValue !== 'go';
}
在您的模板中使用此方法:
<input
[class.disabled]="getValueByIndex(i)"
placeholder="Label"
formControlName="label">
Here's a Working Sample StackBlitz for your ref.
更新:
或者,您可以执行以下操作:
默认禁用 label
字段:
addCreds() {
const creds = this.form.controls.credentials as FormArray;
creds.push(this.fb.group({
action: '',
name: '',
label: {
value: '',
disabled: true
},
state: 'na'
}));
}
然后监听表单上的(ngModelChange)
事件来更新标签字段的状态:
<select
formControlName="action"
(ngModelChange)="($event === 'set' || $event === 'go') ? creds.controls.label.enable() : creds.controls.label.disable()">
<option
*ngFor="let option of data"
[value]="option.key">
{{option.value}}
</option>
</select>
这里有一个 Working Sample StackBlitz 这种方法。
感谢 A.Winnen 对使用先前方法的性能影响的评论。
在 Reactive Form 中你不能使用 disabled 指令。相反,您需要通过调用 enable() 或 disable() 函数来禁用 formControl 'reactive way'。
addCreds() {
const creds = this.form.controls.credentials as FormArray;
const newGroup = this.fb.group({
action: '',
name: '',
label: ''
});
newGroup.controls.action.valueChanges.subscribe((currentAction) => {
if (typeof currentAction === "string" && currentAction.toLowerCase() === 'wait') {
newGroup.controls.label.disable();
} else {
newGroup.controls.label.enable();
}
});
creds.push(newGroup);
}
我修改了你的 stackblitz 示例:https://stackblitz.com/edit/angular-form-array-example-ymysw4