Javascript 角度/业力类型错误:无法读取属性';抵销';未定义的
我写了一个angular指令来操作scroll上的DOM,现在我正试图在Karma/Jasmine中编写一个测试,但我甚至很难通过一个简单的测试,但我还没有找到一个解决方案,我可以应用于我的情况 这是我的指示:Javascript 角度/业力类型错误:无法读取属性';抵销';未定义的,javascript,angularjs,jasmine,karma-runner,karma-jasmine,Javascript,Angularjs,Jasmine,Karma Runner,Karma Jasmine,我写了一个angular指令来操作scroll上的DOM,现在我正试图在Karma/Jasmine中编写一个测试,但我甚至很难通过一个简单的测试,但我还没有找到一个解决方案,我可以应用于我的情况 这是我的指示: 'use strict'; angular.module('myApp.sticky', []) .directive('sticky', ['$window', '$anchorScroll', function($window, $anchorScroll) { re
'use strict';
angular.module('myApp.sticky', [])
.directive('sticky', ['$window', '$anchorScroll',
function($window, $anchorScroll) {
return {
restrict: 'A',
link: function(scope, elm, attrs) {
var atTop;
scope.atTop = atTop;
var stickyVal = elm.context.offsetTop;
scope.sticky = function () {
angular.element($window).bind("scroll", function() {
if (angular.element($window).scrollTop() > stickyVal) {
elm.css({position: 'fixed', top: '0px'});
if (elm.children().length === 1) {
elm.append(elm.next().next());
}
return true;
} else {
elm.css({position: 'static'});
return false;
}
});
};
scope.sticky();
}
};
}]);
这就是我到目前为止的测试结果:
'use strict';
describe('myApp.sticky module', function () {
var scope,
element,
directive,
compiled,
html;
beforeEach(function () {
module('myApp.sticky');
html = '<span sticky>test</span>';
inject(function ($compile, $rootScope) {
scope = $rootScope.$new();
element = angular.element(html);
compiled = $compile(element);
compiled(scope);
scope.$digest();
});
});
describe('sticky directive', function () {
it('should exist', function () {
expect(element).toBeDefined();
});
it('should set the element to a fixed position at a break point',
function () {
});
});
});
任何帮助都将不胜感激
谢谢
更新:通过注释html文件中对jquery的引用,我已经能够让我的代码抛出相同的错误,所以我想知道是否有karma没有得到的依赖项,或者是顺序错误的依赖项?什么是“上下文”?我不知道该如何工作,因为elm应该是原始DOM节点
var stickyVal = elm.context.offsetTop;
将该行更改为:
var stickyVal = elm.offsetTop
var stickyVal = elm.context.offsetTop;
var stickyVal = elm.offsetTop