Javascript AngularJS指令链接函数未在Jasmine测试中运行
在我看来,我有这个指令Javascript AngularJS指令链接函数未在Jasmine测试中运行,javascript,angularjs-directive,jasmine,Javascript,Angularjs Directive,Jasmine,在我看来,我有这个指令 (function() { angular.module('myApp', []) .directive('appFoo', appFoo); function appFoo() { console.log('Directive Factory runs'); return { controller: AppFooController, link: li
(function() {
angular.module('myApp', [])
.directive('appFoo', appFoo);
function appFoo() {
console.log('Directive Factory runs');
return {
controller: AppFooController,
link: link,
replace: true,
restrict: 'E',
scope: {
parentProp: '='
}
};
function AppFooController($scope) {
console.log('Controller runs');
$scope.render({
some: 'data'
});
}
function link($scope, $element) {
console.log('Link function runs');
$scope.render = function(data) {
console.log(shared.$scope.parentProp, $element[0], data);
};
}
}
}());
还有这个茉莉花
describe('myApp::appFoo', function() {
var shared;
beforeEach(function() {
shared = {};
shared.markup = '<app-foo parent-prop="someProp"></app-foo>';
inject(function($compile, $rootScope) {
shared.$compile = $compile;
shared.$parentScope = $rootScope.$new(true);
shared.$rootScope = $rootScope;
});
shared.createDirective = function() {
shared.$element = angular.element(shared.markup);
shared.$compile(shared.$element)(shared.$parentScope);
shared.$parentScope.$digest();
shared.el = shared.$element[0];
shared.$childScope = shared.$element.scope();
};
});
describe('when compiled', function() {
describe('when all parameters are provided', function() {
beforeEach(function() {
shared.$parentScope.someProp = {
a: 'a',
b: 'b'
};
shared.createDirective();
});
it('should have a render method', function() {
expect(typeof shared.$childScope.render).toEqual('function');
});
});
});
});
description('myApp::appFoo',function(){
var共享;
beforeach(函数(){
共享={};
shared.markup=
正如@aliasm2k所指出的,这里的问题是在链接函数之前调用控制器
因为控制器在链接函数创建$scope.render之前调用它,所以调用堆栈被放弃,链接函数没有运行。您确定它在应用程序中工作吗?我非常确定控制器在链接函数之前被调用。因此,您在定义$scope.render之前调用它。这是一个简化的测试用例,但在应用程序中它是有效的-我可能有一个竞争条件,尽管网络参与了应用程序。看看这个,谢谢。