【发布时间】:2018-05-18 11:35:58
【问题描述】:
我正在使用带有反应表单的 Angular 6,我需要在可编辑视图和自定义“只读”视图之间切换表单。项目中有多个自定义输入组件,因此似乎最简单的方法是在 custom-form 元素上使用 [readOnly] 绑定。问题是:
如何在 custom-input 组件中获取父级的 readOnly 值而不直接绑定到每个组件?
例如:
带表格的模板
<custom-form formGroup="formGroup" [readOnly]="!(canEdit$ | async)">
<custom-input formControlName="field1"></custom-input>
<custom-input formControlName="field2"></custom-input>
<custom-select formControlName="field3"></custom-select>
...
</custom-form>
自定义输入模板
// Show input if form readOnly is false
<input *ngIf="!formIsReadOnly"...>
// Show some other representation if not
<span *ngIf="formIsReadOnly">...</span>
【问题讨论】:
标签: angular angular-forms angular-template