I want to apply "ng-pattern" to the form input fields using a directive. Let's say I want to check if the value provided is an integer or not.
form-markup
<form class="form" name="frm" novalidate>
<div class="form-group">
<label class="control-label">Age</label>
<input int
type="text"
name="age"
class="form-control"
ng-model="fd.age">
<span class="help-block" ng-show="frm.age.$error.pattern">
age is just a number.
</span>
</div>
</form>
and the directive code is like this
app.directive("int", function($compile){
return {
restrict: "A",
scope: true,
replace: true,
link: function(scope, elem, attrs) {
elem.attr("ng-pattern", "/[0-9]+/");
}
}
});
In the markup I can see that it is applied properly but still it doesn't work. The pattern is correct because when I use it explicitly in the markup without using the directive it works nicely.
I have two questions.
Why this doesn't work?
Since I have to write a lot of such domain specific directives, is my approach correct to solve this problem pattern.