简体   繁体   English

我可以手动触发ngMessages验证吗?

[英]Can I trigger ngMessages validation by hand?

I have an input field with a custom validator and ng-messages to show validation messages. 我有一个带有自定义验证程序和ng-messages的输入字段,以显示验证消息。

<input type="tel"
       ng-model="ctrl.phoneNumber"
       phone-number
       required />

The custom validator is simple: 定制验证器很简单:

ngModel.$validators.phoneNumber = () => {
    return $iElem.intlTelInput('isValidNumber');
};

For some other events I'd like to trigger validation by hand, also simple 对于其他一些事件,我想手动触发验证,也很简单

$iElem.on('countrychange', () => {
    ngModel.$validate();
});

Which will trigger my custom validator and also this will validate the number again, the form.*.$error object will be updated too, but ngMessages won't reflect, the validation messages won't update somewhy :/ 这将触发我的自定义验证器,这还将再次验证数字,即form.*.$error对象也将被更新,但是ngMessages不会反映出来,验证消息也不会有所更新:/

Any idea? 任何想法?

edit: when I go for the next input in the line ngMessages kicks in for that input AND for the phone-number input as well and the view gets updated, but it's late, like if it would omit one cycle to update the view 编辑:当我在行ngMessages中为该输入和电话号码输入中的下一个输入输入时,视图也将更新,但是视图已更新,但是已经晚了,就像是否忽略一个周期来更新视图

Since the countrychange event is not triggered by angular (ie outside the digest cycle) you'll need to wrap the validate call inside a $scope.$apply . 由于countrychange事件不是由角度触发的(即在摘要周期之外),因此您需要将validate调用包装在$scope.$apply

iElem.on('countrychange', () => {
  $scope.$apply(function(){
    ngModel.$validate();
  })
});

See this discussion for an explanation 请参阅此讨论以获取解释

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

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