指令(隔离作用域、嵌入、替换)将掩码插入到 <body>
中.
var mask = angular.element('<div id="mask"></div>');
$document.find('body').append(mask);
scope.$on('$destroy', function() {
mask.remove();
});
我正在尝试通过范围内的简单广播来测试这种情况:
var $document, scope, element, rootScope;
beforeEach(inject(function($compile, _$document_, $rootScope, $injector) {
rootScope = $injector.get('$rootScope');
scope = $rootScope;
$document = _$document_;
mask = $document.find('#mask');
element = $compile(angular.element('<overlay id="derp"></overlay>'))(scope);
}));
it('should remove mask when casting the $destory event', function (done) {
scope.$broadcast('$destroy');
scope.$digest();
expect($document.find('#mask').length).toBe(0);
});
知道为什么这行不通吗?
最佳答案
错误是:指令创建了 <div id="mask"></div>
multiple 用于多个叠加层。所以 Angular 在添加多个时似乎有问题 <div>
与 DOM 具有相同的 ID。解决此问题后,一切都按预期工作。
it('should remove mask when $destoryed', function () {
scope.$destroy();
expect($document.find('#mask').length).toBe(0);
});
关于javascript - 如何在指令上测试 Angular $destroy 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21246072/