我不得不为应用程序构建相同的组件,虽然它基于OSX提醒计划程序,但它大致相同。
因为可以在整个组件中应用相同的概念,所以最好只关注“结束”选择。该end
FormGroup是在一个更大的FormGroup控制,它看起来像这样
end: this.fb.group({
// default to 'never'. Can also be 'date' or 'count'
selection: 'never',
// default the date to one week from now
date: [startOfDay(addDays(oneHourFromNow, 7)), Validators.required],
// default to only repeating once
count: [1, [Validators.required, CustomValidators.number(this.countLimits)]]
})
很显然,如果你只是提交此表作为-是,它将包含一个值,没有太大的意义。为了抵消这一点,您需要根据需要enable
和disable
date
和count
控件。当抽象控件被禁用时,它将不包含在表单值中。
this.endGroup.get('selection').valueChanges
.startWith(null)
.subscribe(() => {
// if the selection value is 'never',
// disable both the 'date' and 'count' controls
// if the selection value is 'date',
// enable 'date' control and disable 'count' control
// if the selection value is 'count',
// enable 'count' control and disable 'date' control
});
对于最终选择的模板看起来有点像这样,
<mat-select formControlName="selection">...</mat-select>
<ng-container *ngIf="selectionControl.value === 'date'">
<!-- input with datepicker -->
</ng-container>
<ng-container *ngIf="selectionControl.value === 'count'">
<!-- input with type="number" -->
</ng-container>
This article由Todd座右铭在封装形式的群体纳入自己的表象的成分也有帮助。我很乐意解释更多,但它绝对是我必须构建的最复杂的组件,所以包含在单个答案中有点多。
#1听起来像它可以通过简单的ng-if或ng-show来处理。也许你可以显示一些代码? –