Javascript 为什么可以';i访问指令';茉莉花试验的适用范围?
这是我的指示:Javascript 为什么可以';i访问指令';茉莉花试验的适用范围?,javascript,angularjs,jasmine,Javascript,Angularjs,Jasmine,这是我的指示: angular.module('app') .directive('statusFilter',statusFilter); function statusFilter() { return { restrict: 'E', replace: true, templateUrl: 'app/directives/status-filter.html', scope: { fl
angular.module('app')
.directive('statusFilter',statusFilter);
function statusFilter() {
return {
restrict: 'E',
replace: true,
templateUrl: 'app/directives/status-filter.html',
scope: {
flags: '='
},
controller: function($scope, $element, $timeout, $document) {
function isChildElement(el) {
return $.contains($element[0], el);
}
function close(event) {
if (!isChildElement(event.target)) {
$scope.$apply(function() {
$scope.isOpen = false;
});
$document.off('mouseup', close);
}
}
function updateFlags(value) {
for (var prop in $scope.flagsClone) {
$scope.flagsClone[prop] = value;
}
}
function pullFlags() {
$scope.flagsClone = $.extend(true, {}, $scope.flags);
}
function pushFlags() {
for (var prop in $scope.flagsClone) {
$scope.flags[prop] = $scope.flagsClone[prop];
}
}
$scope.isOpen = false;
$scope.flagsClone = {};
pullFlags();
$scope.apply = function() {
pushFlags();
$scope.isOpen = false;
};
$scope.selectAll = function() {
updateFlags(true);
};
$scope.selectNone = function() {
updateFlags(false);
};
$scope.open = function() {
if (!$scope.isOpen) {
pullFlags();
$scope.isOpen = true;
$timeout(function() {
$document.on('mouseup', close);
});
}
};
}
};
}
下面是我为它编写的一个简单测试:
describe('status-filter directive', function() {
beforeEach(module('app'));
var template = '<status-filter flags="filters"></status-filter>';
var scope, element;
beforeEach(inject(function($rootScope, $compile) {
scope = $rootScope.$new();
var ngElement = angular.element(template);
element = $compile(ngElement)(scope);
scope.$digest();
}));
it('Should open when isOpen is true', function() {
scope.open();
scope.$digest();
expect(scope.isOpen).toBe(true);
});
});
description('status-filter指令',function()){
在每个(模块(“应用”)之前;
var模板=“”;
var范围、要素;
beforeach(注入(函数($rootScope,$compile){
scope=$rootScope.$new();
var ngElement=角度元素(模板);
元素=$compile(ngElement)(范围);
范围。$digest();
}));
它('当isOpen为true时应打开',函数(){
scope.open();
范围。$digest();
expect(scope.isOpen).toBe(true);
});
});
无论我如何尝试,我都无法访问指令的范围。与上面的示例一样,.isolateScope(),element.scope()。无论我尝试什么,我得到的
open()都是未定义的
错误。我的代码有什么问题?我无法访问范围的原因是我没有创建filters变量。因此,这将起作用:
describe('status-filter directive', function() {
beforeEach(module('app'));
var template = '<status-filter flags="filters"></status-filter>';
var scope, element;
beforeEach(inject(function($rootScope, $compile) {
scope = $rootScope.$new();
scope.filters = {
filter1:true;
}
var ngElement = angular.element(template);
element = $compile(ngElement)(scope);
scope.$digest();
}));
it('Should open when isOpen is true', function() {
scope.open();
scope.$digest();
expect(scope.isOpen).toBe(true);
});
});
description('status-filter指令',function()){
在每个(模块(“应用”)之前;
var模板=“”;
var范围、要素;
beforeach(注入(函数($rootScope,$compile){
scope=$rootScope.$new();
scope.filters={
过滤器1:正确;
}
var ngElement=角度元素(模板);
元素=$compile(ngElement)(范围);
范围。$digest();
}));
它('当isOpen为true时应打开',函数(){
scope.open();
范围。$digest();
expect(scope.isOpen).toBe(true);
});
});