I have a multi-purpose modal dialog in my app with one text input. Sometimes I need just any text, sometimes I want to restrict the text to match a regex pattern.
I tried adding the regex with jQuery attr
like this:
$("#popupTextInput").attr("ng-pattern", "/.*imdb\.com\/title\/.*/");
Doesn't work. When I inspect the rendered element the correct ng-pattern
directive appears on the element but the formname.$valid
is always true, no matter what I enter.
When I manually add the attribute (with the exact same appearance in firebug) it works as expected.
Instead of a string with the pattern I also tried adding a regex pattern directly to the attribute like this:
var regexp = /.*imdb\.com\/title\/.*/;
$("#popupTextInput").attr("ng-pattern", regexp);
But that doesn't work either.
I am puzzled. Can anybody shed some light on this? I should probably mention that I add the attribute before I create the modal.
ETA: Did some further experiments. I tried having ng-pattern on the input by default and remove when not needed. Something similar happened: When I remove the ng-pattern it is removed from the element (at least inspection doesn't show it) but the regex pattern is still in effect. I tried the $scope.$apply suggestion below (with and without function in $apply) but it has no effect.