我收到以下异常'未知提供商:authSvc'这是源于我的config.js文件,我将在下面发布。我理解这个错误意味着我的authSvc模块尚未注册且不可用,但是在我尝试注入之前创建了服务。有人可以帮帮我吗?我迷失在这一个......所以我想转向SO。感谢。
的index.html
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<!-- Place favicon.ico in the root directory -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<!-- <script src="js/vendor/modernizr-2.8.3.min.js"></script> -->
<script data-main="js/main" src="js/vendor/require.js"></script>
</head>
<body>
<!--[if lt IE 8]>
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<nav class="navbar navbar-inverse navbar-fixed-top" ng-controller="navCtrl">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li><button class="btn btn-default navbar-btn" ng-click="login()">Login</button></li>
<li><button class="btn btn-info navbar-btn" ng-click="register()">Register</button></li>
</ul>
</div><!--/.navbar-collapse -->
</div>
</nav>
<div ng-view></div>
</body>
</html>
main.js
require.config({
paths: {
'angular': 'vendor/angular',
'domReady': 'vendor/domready',
'angularRoute': 'vendor/angular-route',
'angularLocalStorage': 'vendor/angular-local-storage',
'bootstrapCss': 'vendor/bootstrap-css'
},
shim: {
'angular': {
exports: 'angular'
},
'angularRoute': {
deps: ['angular']
},
'angularLocalStorage': {
deps: ['angular']
}
},
deps: ['./bootstrap']
});
bootstrap.js
require([
'angular',
'app',
'services/authSvc',
'controllers/navCtrl',
'controllers/landingPageCtrl',
'controllers/homeCtrl',
'controllers/loginCtrl',
'config',
'routes'
], function (angular) {
'use strict';
angular.element(document).ready(function () {
angular.bootstrap(document, ['myApp']);
});
});
app.js
define([
'angular',
'angularRoute',
'angularLocalStorage'
], function(angular, angularRoute, angularLocalStorage) {
'use strict';
var app = angular.module('myApp', ['ngRoute', 'LocalStorageModule']);
return app;
});
authSvc.js
define(['app'], function(app) {
app.service('authSvc', function($http, localStorageService) {
this.setToken = function(token) {
return localStorageService.set('token', token);
}
this.getToken = function() {
return localStorageService.get('token');
}
this.login = function(username, password) {
return $http.post('/api/login', {username: username, password: password});
}
});
console.log('authSvc registered');
});
config.js
define(['app'], function(app) {
'use strict';
app.config(function($httpProvider, authSvc) {
$httpProvider.interceptors.push(function($q) {
return {
'request': function(config) {
// same as above
//config.headers.token = authSvc.getToken();
return config;
},
'response': function(response) {
// same as above
return response;
}
};
});
});
console.log('config registered');
});
答案 0 :(得分:1)
您无法在配置块中注入服务。只有供应商。
app
.factory('yourInterceptor', yourInterceptor)
.config(configureInterceptor);
function configureInterceptor($httpProvider) {
$httpProvider.interceptors.push('yourInterceptor');
}
function yourInterceptor(authSvc) {
return {
'request': function (config) {
// same as above
//config.headers.token = authSvc.getToken();
return config;
},
'response': function (response) {
// same as above
return response;
}
};
}
编辑:
将进样器传递给拦截器并使用进样器获取服务实例
function yourInterceptor($injector) {
return {
'request': function (config) {
var authService = $injector.get('authSvc');
//config.headers.token = authSvc.getToken();
return config;
},
'response': function (response) {
// same as above
return response;
}
};
}