简体   繁体   English

没有覆盖ngform时,角度2检查有效性

[英]angular 2 check validity when no covering ngform

  <input  matInput  [(ngModel)]="testDate" (ngModelChange)="mychange($event)" [value]="minDate" [min]="minDate" [max]="maxDate" [matDatepicker]...

I dont have a covering ngform or submit button. 我没有ngform或提交按钮。 In myChange() how can I check if testDate is valid. 在myChange()中,如何检查testDate是否有效。 (just like if there was a covering form/control group we can do form.isvalid..to enable/disable the submit button (就像有一个覆盖表单/控制组一样,我们可以执行form.isvalid ..以启用/禁用提交按钮

I think that the best way for you to do that is to put your field inside a form. 我认为最好的方法是将您的字段放在表单中。 But you can work around it, and add some validation for the change event, maybe testing if it is null, or if it is an empty string or if it's a valid date object. 但是您可以解决该问题,并为change事件添加一些验证,例如可以测试它是否为null,是否为空字符串或是否为有效的date对象。 Then, use a Boolean field and assign to it the result of the validation and use that field on your button. 然后,使用布尔值字段并将验证结果分配给它,并在按钮上使用该字段。

<button (click)="callMyCallBackFunction($event)" [disabled]="myValidationInputField"></button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM