简体   繁体   中英

Unable to remove element in angular directive

I have written following code snippet for an authentication directive:

angular.module('authDirective',[]).directive('hiAccess', ['authService', function(authService){
    return{
        restrict: 'A',
        compile:  function(element, attr){

            var userRole = authService.getUserRole();

            var allowedAccess = attr.hiAccess.split(" ");

            outerLoop: for(access in allowedAccess){
                for(role in userRole){
                    if(access == role) {
                        /*angular.forEach(element.children(), function(child){
                            //child.remove();
                        });*/
                        element.remove();
                        break outerLoop;
                    }
                }
            }

        }
    }
}]);

But I am getting following error in browser because of element.remove():

TypeError: Cannot read property 'childNodes' of undefined

Any suggestions ?

我在指令中使用了链接功能,而不是在编译时使用它,现在看来工作正常。

u can try

function removeElement(elem, wait) {
     ripples.splice(ripples.indexOf(elem), 1);
     if (ripples.length === 0) {
         getRippleContainer().css({ backgroundColor: '' });
     }
     $timeout(function () {
         elem.remove();
     }, wait, false);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM