Jim-Y Jim-Y - 2 years ago 85
AngularJS Question

Can I trigger ngMessages validation by hand?

I have an input field with a custom validator and ng-messages to show validation messages.

<input type="tel"
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', () => {

Which will trigger my custom validator and also this will validate the number again, the
object will be updated too, but ngMessages won't reflect, the validation messages won't update somewhy :/

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

Answer Source

Since the countrychange event is not triggered by angular (i.e. outside the digest cycle) you'll need to wrap the validate call inside a $scope.$apply.

iElem.on('countrychange', () => {

See this discussion for an explanation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download