我有一个控制器中有一个调用的功能
var someVar = angular.element(event.target).scope().field;
我试图通过做嘲笑它
var ngElementFake = function(el) { return { scope: function() { return { toggleChildElement: true,field: scope.field } } } } spyOn(angular,'element').andCallFake(ngElementFake);
TypeError: 'undefined' is not a function (evaluating 'injector.get('$rootElement').off()') at ../angular-mocks/angular-mocks.js:1819
我究竟做错了什么?
编辑:注射
beforeEach(function() { inject(function($rootScope,$controller) { scope = $rootScope; scope.record = recordData; scope.model = 'Hierarchy'; ctrl = $controller("fngHierarchyChildCtrl",{ $scope: scope }); }); });
我能够通过在回调后手动清除间谍来解决这个问题.
原文链接:https://www.f2er.com/angularjs/142848.htmlvar spy; beforeEach(function() { spy = spyOn(angular,'element').andCallFake(ngElementFake); }); afterEach(function() { spy.andCallThrough(); });