I want to check the changing of a value in a custom directive.
To do this, I use $parsers unshift-function to add my own function
But, my own function is not called!
This is my view:
<div ng-controller="MyCtrl">
<form novalidate name="myForm">
Number: <even-number name="awesomeField" ng-model="val"></even-number>
</form>
</div>
This is my javasript code:
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.val = "42";
$scope.$watch('val', function() {
console.log("Controller: "+$scope.val);
});
}
myApp.directive('evenNumber', function(){
var tmplt = ''+
'<div class="input-group">'+
'<input class="form-control" name="inputDate" type="text" data-ng-model="ngModel"/>'+
'<span class="input-group-btn">'+
'<button class="btn btn-default">Default</button>'+
'</span>'+
'</div>';
return {
restrict: 'E',
require:'ngModel',
replace: true,
template: tmplt,
scope: {
ngModel: "="
},
link: function(scope, elem, attrs, ctrl){
ctrl.$parsers.unshift(checkValue);
function checkValue(viewValue){
console.log("halllllo");
return viewValue;
}
} // end link
}; // end return
});
What's the problem here?
Here it is as a jsFiddle