我一直在撓撓我的頭這幾個小時了,經過大量的搜索後,我還沒有找到有用的解決方案。角度路線沒有解決服務
作爲標題狀態,我的依賴關係沒有被Angular的路由提供者解決。這是錯誤:
Unknown provider: testServiceProvider <- testService <- testService
我編譯JavaScript文件(app.js
)看起來是這樣的:
'use-strict';
var app = angular.module('test-app', ['ngRoute']);
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/home.html',
controller: 'HomeController',
resolve: {
testService: function (testService) {
console.log(testService.message);
}
}
})
}]);
app.factory('apiService', ['$http', function ($http) {
function url(endpoint) {
return '/api/v1' + endpoint;
}
return {
user: {
authenticated: function() {
return $http({method: 'GET', url: url('/user/authenticated')});
},
login: function (token) {
return $http({method: 'GET', url: url('/user/login'), cache: false, headers: {
Authorization: 'Basic ' + token
}});
},
logout: function() {
return $http({method: 'GET', url: url('/user/logout')});
},
model: function() {
return $http({method: 'GET', url: url('/user/data')});
}
}
};
}]);
app.factory('testService', function() {
return {
message: 'Hello world'
};
});
app.controller('HomeController', ['$scope', '$http', function ($scope, $http, apiService, testService) {
$scope.user = {
authenticated: false,
error: '',
username: '',
password: ''
};
$scope.login_button = 'Log in';
$scope.isAuthenticated = function() {
return $scope.user.authenticated;
}
$scope.needsAuthentication = function() {
if (!$scope.user.authenticated) {
return true;
}
return false;
}
$scope.logIn = function ($event) {
$event.preventDefault();
$scope.login_button = 'Please wait...';
var token = btoa($scope.user.username + ':' + $scope.user.password);
apiService.user.login(token).then(function (success) {
$scope.user = success.data.user;
$scope.user.authenticated = true;
}, function (error) {
$scope.user.error = 'Please try logging in again.';
$scope.login_button = 'Log in';
});
};
}]);
至於我可以告訴大家,一切都應該被解決的罰款;我是否錯過或誤解了某些東西?
我想在別名的問題。您使用testService作爲解析的別名。注射器可能會混淆。嘗試將其重命名爲例如:解析:testData:function(testService){console.log(testService.message); } }'並在控制器中重命名。 – Errorpro
@Errorpro它返回基本上相同的錯誤:'未知的提供者:testServiceProvider < - testService < - testData'。 – P810
@Errorpro實際上,我相信當我發佈最後一條評論時,肯定有單獨的錯誤原因,因爲現在它正在工作。謝謝! – P810