所以我正在学习如何使用业力。我已经阅读了几个关于如何将范围注入我的项目的教程,但我仍然遇到错误
错误:[$ injector:unpr]未知提供者:$ scopeProvider< - $ scope http://errors.angularjs.org/1.6.3/ $注射器/ unpr?P0 =%24scopeProvider%20%3 C-%20%24scope
这是我的规格:
describe('InfoController', function() {
var InfoController, state;
beforeEach(angular.mock.module('ptoApp.controller'));
var $controller
var scope;
beforeEach(inject(function($controller,$scope, $state,$rootScope, employeeTestFactory) {
scope = $rootScope.$new();
InfoController = $controller('InfoController', {
$scope: scope,
state: $state,
employeeTestFactory: employeeTestFactory
});
}));
// Verify our controller exists
it('should be defined', function() {
expect(InfoController).toBeDefined();
});
});
这是我的控制器:
.controller('InfoController', ['$scope', '$state','$rootScope', 'employeeTestFactory', function ($scope, $state, $rootScope, employeeTestFactory) {
$scope.loggedIn = {};
$scope.displaySection = true;
$rootScope.callInfo=function(){
employeeTestFactory.get($rootScope.email).then(function(result) {
$scope.loggedIn = result;
});
}
$scope.getPercentUsed = function(){
if(typeof $rootScope.employee === 'undefined' || typeof $rootScope.employee.totalTimeUsed === 'undefined' || typeof $rootScope.employee.totalTimeAccrued === 'undefined') return 0;
var percentage = ($rootScope.employee.totalTimeUsed/$rootScope.employee.totalTimeAccrued)*100;
return (percentage > 100) ? 100 : percentage;
}
$scope.hideSession = function(){
$scope.displaySection = !$scope.displaySection;
}
}])
现在我听说过范围,立即将新的rootScope附加到它是有用的,所以我添加了scope = $ rootScope。$ new();然后在创建控制器时将其设置为$ scope:scope。仍然得到错误。 在此先感谢您的帮助。
答案 0 :(得分:1)
在您的业力规范文件中,从$scope
函数参数中删除inject
:
- beforeEach(inject(function($controller,$scope, $state,$rootScope, employeeTestFactory) {
+ beforeEach(inject(function($controller, $state, $rootScope, employeeTestFactory) {