2014-09-05 64 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