2014-09-05 66 views
0

每次添加元素时,都会再次触发该指令。我只是想将elem纳入icr主模态布局元素。防止角JS指令在移动元素时触发两次

.directive('icrMainModal', function() { 
    return { 
    restrict: 'E', 
    templateUrl: 'views/icr-main-modal.html', 
    scope: { 
     state: '=icrVState' 
    }, 
    transclude: true, 
    compile: function(elem, attrs) { 

     angular.element('icr-main-modal-placement').append(elem); // because of this, the directive is fired again. I just want to move elem into 'icr-main-modal-placement' 

     return function(scope, elem) { 

     scope.$on('$destroy', function() { 
      return elem.remove(); 
     }); 

     }; 

    } 
    }; 
}); 
+0

你可以显示你的HTML如何使用它? – PSL 2014-09-05 20:09:06

回答

1

我能想到的一种方法是为Attribute指定此指令而不是Element,并在编译/链接方法中删除属性?

.directive('icrMainModal', function() { 
    return { 
    restrict: 'A', 
    templateUrl: 'views/icr-main-modal.html', 
    scope: { 
     state: '=icrMainModal' 
    }, 
    transclude: true, 
    compile: function(elem, attrs) { 
     elem.removeAttr('icr-main-modal'); 
     angular.element('icr-main-modal-placement').append(elem); 

     return function(scope, elem) { 

     scope.$on('$destroy', function() { 
      return elem.remove(); 
     }); 

     }; 
    } 
    }; 
}); 
+0

是啊!这也是我想到的,看起来似乎只是一种解决方法,而不是解决方案(可能这不是一个常见问题),这真是糟糕透顶。但是这对我很有用,谢谢你的关注! – TaylorMac 2014-09-06 18:49:27