我有一个文本框角色,我想检查角色是否已经存在于数据库或不在服务器端为我已经创建了一个指令uniqueRole现在我想通过文本框值该指令如何做到这一点。如何将模型值传递给自定义指令?
**HTML**
<input type="text" class="role-textbox" id="rolename" name="rolename" required ng-model="roledetails.name" unique-role send-value="roledetails.name" placeholder="{{::'placeholder.addRole.name'|translate}}">
**Controller**
'use strict';
define([
'angular',
'./module',
], function(angular, directives) {
directives.directive('uniqueRole', function($timeout, $q, restClientTemplate) {
return {
restrict: 'AE',
require: 'ngModel',
scope:{
sendValue: '='
},
link: function(scope, elm, attr, model) {
model.$asyncValidators.usernameExists = function(roledata) {
var defer = $q.defer();
console.log("gng to call controller");
restClientTemplate.execute({
method: 'POST',
url: 'json/check/role',
data: roledata
}).then(function(response) {
roledata = response.results;
defer.resolve(roledata);
}, function(error) {
defer.reject(error);
});
$timeout(function() {
model.$setValidity('usernameExists', false);
defer.resolve;
}, 1000);
return defer.promise;
};
}
}
});
});
我还是编辑的代码不工作仍然有价值,因为undefined请帮助 –