我有一个表单,我正在将我的电话字段从使用正则表达式更改为intl-tel输入模块.我遇到问题在检查有效#s时混淆.
我有一个表单与这些领域的几个…
<div class="row"> <div class="col-xs-2"> <label for="cellPhone" translate>CONTACT.CELL</label> </div> <div class="col-xs-6" ng-class="{'has-error': !cellPhoneFocus && forms.contactForm.cellPhone.$invalid && forms.contactForm.cellPhone.$touched }"> <input type="text" class="form-control intlPhoneInput" id="cellPhone" name="cellPhone" ng-model="contact.cellPhone.display" ng-focus="cellPhoneFocus = true" ng-blur="cellPhoneFocus = false; validatePhone($event)"> <div ng-messages="forms.contactForm.cellPhone.$error" ng-show="!cellPhoneFocus && forms.contactForm.cellPhone.$touched" class="errorMessages"> <p ng-message="pattern" translate> CRUD.VALID_PHONE</p> </div> </div>
并提交…
<button type="submit" class="btn btn-primary" ng-disabled="forms.contactForm.$invalid" id="saveContactLink" translate>CRUD.SAVE</button>
然后在我的控制器(Typescript)…
//In Constructor $scope.validatePhone = this.validatePhone.bind(this); //Outside constructor private validatePhone(eventObject: any) { let thePhoneField = $('#' + eventObject.target.id); let phoneIsValid = thePhoneField.intlTelInput("isValidNumber"); this.$scope.forms.contactForm[eventObject.target.id].$invalid = !phoneIsValid; this.$scope.forms.contactForm[eventObject.target.id].$error = {"pattern": !phoneIsValid}; }
这样可以正确设置我的错误类和错误消息,但不会将FORM设置为无效.我试过$setValidity …
this.$scope.forms.contactForm[eventObject.target.id].$setValidity('pattern',!phoneIsValid);
…但似乎根本不做任何事情.
如何将该字段设置为无效,显示正确的ng消息(如果有多个),并确保窗体$errors更新,以便提交被禁用?
结果是$setValidity工作,但具有setValidity.$invalid和$error导致一些交叉的线.在使字段无效时,注释最后两个启用/禁用我的提交按钮.
原文链接:https://www.f2er.com/angularjs/140657.html