我已經聲明的自定義指令,以檢查用戶的所有腦幹中angularjs困難而使用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>
當我嘗試訪問訪問$範圍在myController中輸入sponsorID的值,它表示「undefined」
app.controller('myController', function($scope, $http, $routeParams) {
console.log($scope.sponsorID);
});
如何訪問sp的值onsorID當定製定製指令時
你有沒有嘗試設置ngmodel做的指令綁定:'範圍:{...,ngModel: '=',...}' – Vinny