Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

input does not watch ngPattern value for changes #5226

Closed
taralx opened this issue Dec 2, 2013 · 8 comments
Closed

input does not watch ngPattern value for changes #5226

taralx opened this issue Dec 2, 2013 · 8 comments

Comments

@taralx
Copy link

taralx commented Dec 2, 2013

If you use input ng-pattern="var" with $scope.var = some regexp, and then you change that regexp in a handler, the validation doesn't react until the user modifies the field.

I'm working around this by using this hack:

var c = angular.element(document.getElementById('id')).controller('ngModel');
c.$setViewValue(c.$viewValue);

@IgorMinar
Copy link
Contributor

@caitp this is kind of related to the validation stuff that you've been working on elsewhere, would you like to take a look at this one as well?

@IgorMinar
Copy link
Contributor

the tricky thing here is that we should revalidate when the pattern changes in addition to when the viewValue changes

@caitp
Copy link
Contributor

caitp commented Dec 21, 2013

The thing with this is that currently the pattern doesn't come from a parsed expression / scope variable, it's just a string literal turned into a regexp, so watching for changes to that means essentially watching the DOM attribute value for change. I think I mentioned that on a different issue regarding this a few weeks ago. Watching changes to the actual DOM attribute is fairly different from what angular is typically doing. Watching the actual DOM attribute value also means that it's not really possible to unwatch constant expressions, which is kind of sad.

So, there are ways to do it, but I'm not totally sure about them being good ideas. If it's a green light for breaking change, then that's one thing, but otherwise I'm not too sure about it.

@taralx
Copy link
Author

taralx commented Dec 21, 2013

The pattern does come from a scope variable when ng-pattern is "var" instead of "/regexp/". I'm not changing the DOM in my example above.

@royts
Copy link

royts commented Mar 2, 2014

+1

@thelgevold
Copy link

I think it would be very useful to allow binding to ng-pattern. Perhaps make it a standalone directive as suggested above.
One use case that comes to mind is that it would allow us to define complex regex expressions in services/controllers instead of hard coding the expression in the markup. This will allow us to unit test the expressions, but also make reuse easier.

@alexander-87
Copy link

+1

@Narretz Narretz self-assigned this Feb 9, 2015
@Narretz
Copy link
Contributor

Narretz commented Feb 11, 2015

It actually does that in >= 1.3.x:

http://plnkr.co/edit/QNX5dXmRsBMYlMle9LHH

Enter some characters from A-Z -> model is set. Then clichk switch, and the model will be invalidated. ngPattern is also already a standalone directive.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants