【发布时间】:2014-07-10 02:21:03
【问题描述】:
我不明白为什么在下面的示例中没有触发 $destroy 事件。谁能解释一下为什么不触发,什么情况下会触发?
这里是 plunkr:http://plnkr.co/edit/3Fz50aNeuculWKJ22iAX?p=preview
JS
angular.module('testMod', [])
.controller('testCtrl', function($scope){
$scope.removeElem = function(id) {
var elem = document.getElementById(id);
angular.element(elem).remove();
}
}).directive('testDir',[function() {
return {
scope:true,
link: function(scope) {
console.log('in directive');
scope.$on('$destroy', function(){
alert('destroyed');
})
}
}
}]);
HTML
<body ng-controller='testCtrl'>
<div testDir id='test'>I will be removed.</div>
<button ng-click='removeElem('test')'>remove</button>
</body>
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope