我想在路线上设置resolve属性。当路线为/home
时,应设置test
- 如下所示:
.when('/home', {
controller: 'home',
templateUrl: "application/views/Angular_spa/angular_templates/homepage.html",
resolve: {
test: function () {
return 'test variable';
}
}
})
home
控制器代码如下:
application.controller('home', function ($scope, $routeParams, $http, contentService, newsService, announceService, test) {
newsService.getAllNews(function (data) {
$scope.news = data; //U scopeu imamo novosti
});
announceService.getAllAnnouncments(function (data) {
$scope.announcments = data;
});
$scope.siteURL = URLServer;
})
我收到以下错误:
Error: [$injector:unpr] http://errors.angularjs.org/1.2.15/$injector/unpr?p0=testProvider%20%3C-%20test
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:6:450
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:34:510
at Object.c [as get] (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:33:83)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:35:59
at c (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:33:83)
at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:33:300)
at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:33:464)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:65:486
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:52:156 <div ng-view="" class="centersection ng-scope">
为什么我无法将test
从解决方案传递到home
控制器并稍后将其分配给$scope.something
属性,我做错了什么?
以下是模块定义:
var application = angular.module('content', ['ngRoute', 'ngSanitize'])
.config(function ($routeProvider) {
$routeProvider
.when('/home/content/:contentId', {
controller: 'content',
templateUrl: "application/views/Angular_spa/angular_templates/content.html"
})
.when('/home', {
redirectTo: '/'
})
.when('/', {
controller: 'home',
templateUrl: "application/views/Angular_spa/angular_templates/homepage.html"
resolve: {
test: function () {
return 'test variable';
}
}
})
.when('/about-us', {
controller: 'aboutus',
templateUrl: "application/views/Angular_spa/angular_templates/content.html"
})
.when('/contact-us', {
controller: 'contactus',
templateUrl: "application/views/Angular_spa/angular_templates/content.html"
})
.when('/branch/:branchId', {
controller: 'branch',
templateUrl: "application/views/Angular_spa/angular_templates/branch.html"
})
.when('/home/downloads', {
controller: 'downloads',
templateUrl: "application/views/Angular_spa/angular_templates/download.html"
})
.when('/home/feedback', {
controller: 'feedback',
templateUrl: "application/views/Angular_spa/angular_templates/feedback.html"
})
});