我有一个工作正常的自定义指令,直到我向其中添加了一些服务,却给了我一个“错误:ng:areq Bad Argument”无法弄清楚原因。
我的指令:
angular.module('myApp')
.directive('modalContent',[
'$uibModal',
'UserService'],
function($uibModal, UserService){
return {
restrict: 'A',
priority: 100,
link: function($scope, element, attrs) {
element.on( 'click', function( evt ){
console.log("Click and Stop");
evt.preventDefault()
evt.stopImmediatePropagation();
$scope.$apply(function(){
console.log("call Modal");
});
});
}
};
});
最佳答案
在创建指令之前尝试初始化 Angular 模块:
angular.module('myApp',[]);
之后,您就可以使用您的代码了。
<小时/>编辑:
该错误是由于语法错误造成的,在'UserService'
之后有一个额外的]
,该指令的正确定义是:
angular.module('myApp')
.directive('modalContent',[
'$uibModal',
'UserService',
function($uibModal, UserService){
return {
restrict: 'A',
priority: 100,
link: function($scope, element, attrs) {
element.on( 'click', function( evt ){
console.log("Click and Stop");
evt.preventDefault()
evt.stopImmediatePropagation();
$scope.$apply(function(){
console.log("call Modal");
});
});
}
};
}]);
还要注意最后的变化:}]);
而不是 });
关于javascript - 在自定义指令中调用服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38506899/