I've declared a custom directive to check the existance of user in angularjs
var app = angular.module('myApp', []);
app.factory('Contestant',function($http){
return {
checkUser : function(email){
return $http.post('ajax/checkuser.php',{email:email});
}
}
});
app.directive('checkUser',function(Contestant) {
return {
require: 'ngModel',
link: function(scope, element, attrs,ctrl) {
ctrl.$parsers.unshift(function(viewValue) {
Contestant.checkUser(viewValue).success(function (response) {
ctrl.$setValidity('checkUser', true);
return viewValue;
})
.error(function (data) {
ctrl.$setValidity('checkUser', false);
return undefined;
});
});
}
}
});
HTML
<html ng-app="myApp">
<form name="signup_form" data-ng-controller="myController" novalidate>
<input type="text" id="sponsor" name="sponsor" ng-model="sponsorID" check-user required />
</form>
</html>
Now when i try to access the value of sponsorID input in myController, it says "undefined"
app.controller('myController', function($scope, $http, $routeParams) {
console.log($scope.sponsorID);
});
How to access the value of sponsorID while the custom directive in place
scope: { ..., ngModel: '=', ... }
– Vinny Dec 11 '13 at 18:41