【发布时间】:2017-11-02 12:44:28
【问题描述】:
我有一个指令来监视组件的变化,并在值发生变化时调用自定义验证器。
为什么ngModel.valueChanges在组件初始化的时候会有新的变化?
@Directive({
providers: [NgModel],
selector: "[inputValidator][ngModel]",
})
@Inject(BootstrapFormGroupComponent)
export class InputValidatorDirective implements OnInit, BootstrapFormGroupMember {
public ngOnInit() {
this.ngModel.valueChanges.subscribe((value) => {
// ...
// calling custom validators
// why it is called after the component has initialized
// ...
});
}
}
HTML:
<input type="number" inputValidator [validator]="postalCodeValidator" [(ngModel)]="postalCode" class="form-control" />
【问题讨论】:
-
你如何使用它?显示html
-
postalCode的初始值是多少? -
我尝试过使用 null、undefined 和 ""