Javascript element.remove不';行不通
我试图实现一个指令,根据用户角色从页面中删除元素 这是我的指令:Javascript element.remove不';行不通,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我试图实现一个指令,根据用户角色从页面中删除元素 这是我的指令: angular.module("BlurAdmin") .directive('restrict',restrict); function restrict (authService, $http, $state){ return{ restrict: 'A', prioriry: 100000, scope: false, link: function(){ }
angular.module("BlurAdmin")
.directive('restrict',restrict);
function restrict (authService, $http, $state){
return{
restrict: 'A',
prioriry: 100000,
scope: false,
link: function(){
},
compile: function(element, attr, linker){
var accessDenied = true;
var attributes = attr.access.split(" ");
var getCurrentUser = function () {
authService.getCurrentUser()
.success(function (data) {
var user = data;
for(var i in attributes){
if(user.roles[0].name == attributes[i]){
accessDenied = false;
}
};
if(accessDenied){
element.children().remove();
element.remove();
};
})
.error(function (data, status) {
$state.go("/login");
});
};
getCurrentUser();
}
}
}
以下是我访问它的方式:
<input type="text" id="title" ng-model="reunion.title" restrict access="SUPERCHEF" required>
----------->元素正在被删除
但是当我添加$http调用时,元素并没有被删除。这似乎很好。你确定代码会进入if块,元素会被删除吗?是的,我确定我尝试过它可能是一个范围问题?只是为了测试,你能在link函数中做同样的操作而不是编译吗?我刚刚做了,指令甚至没有被调用
angular.module("BlurAdmin")
.directive('restrict',restrict);
function restrict (authService, $http, $state){
return{
restrict: 'A',
prioriry: 100000,
scope: true,
link: function(){
// alert('ergo sum!');
},
compile: function(element, attr, linker){
var accessDenied = true;
var attributes = attr.access.split(" ");
console.log("remove elem");
element.children().remove();
element.remove();
}
}
}