AngularJS routeProvider 控制器中的依赖注入解析
AngularJS Dependency injection in controller of routeProvider resolve
注入 resolve
的 return 对象,应该注入到控制器中,但事实并非如此。我正在使用一个承诺,以便控制器在承诺得到解决之前不会实例化,因为我正在使用 $http
服务执行 HTTP POST。
我也在使用 $location.path
根据响应数据进行一些重定向。第一条路由将使用它获得的数据重定向到 template1
路由。
$routeProvider
.when("/auth/:id", amd.route(
{
controller: 'eventController',
controllerUrl: 'app/controllers/eventController',
resolve: {
returnedData: ['$http', '$location', '$route', '$q', function ($http, $location, $route, $q) {
var id = $route.current.params.id;
var defer = $q.defer();
$http({
method: 'POST',
url: 'http://domain/service/webapi',
data: { 'name': id }
}).then(function (response) {
var template = "/template".concat(response.data.displayLayout);
defer.resolve(response.data);
$location.path(template);
return defer.promise;
});
}]
}
}))
.when("/template1", amd.route(
{
templateUrl: 'views/templates/template1.html',
controller: 'eventController',
controllerUrl: 'app/controllers/eventController',
}));
app.controller('eventController', ['$scope', '$routeParams', '$timeout', 'returnedData',
function ($scope, $routeParams, $timeout, returnedData) {
// ...controller code here: $scope.returnedData = returnedData
});
我收到 AngularJS 错误:
Error: $injector:unpr Unknown Provider
对此有什么想法吗?
在AngularJSAPI页面,关于routeProvider(https://docs.angularjs.org/api/ngRoute/provider/$routeProvider)
你可以看到:
resolve - {Object.=} ... If any of these dependencies are promises, the router will wait for them all to be resolved or one to be rejected before the controller is instantiated
您应该像下面这样编辑您的代码:
var id = $route.current.params.id;
var defer = $q.defer();
$http({
method: 'POST',
url: 'http://domain/service/webapi',
data: { 'name': id }
}).then(function (response) {
var template = "/template".concat(response.data.displayLayout);
defer.resolve(response.data);
$location.path(template);
});
// return the promise outside `then` method, then angularjs will wait it `resolve`
return defer.promise;
此外,请注意:
Be aware that ngRoute.$routeParams will still refer to the previous route within these resolve functions. Use $route.current.params to access the new route parameters, instead.
使用 ng-route 的依赖注入,
我的解决方案:
var myApp = angular.module("myApp", ["ngRoute", "localytics.directives"]);
myApp.config(['$routeProvider',function ($routeProvider) {
$routeProvider
.when("/", {
templateUrl: "//TimeLine.html",
controller: "MainCtrl"
})
.when("/AddOrEditOccasion", {
templateUrl: "/Html/GiftWhiz/AddOrEditOccasion.html",
controller: "AddOrEditOccasionCtrl"
})
.when("/OccasionStart", {
templateUrl: "/OccasionStart.html",
controller: "OccasionStartCtrl"
})
}]);
myApp.controller("MainCtrl", ['$scope', '$http', '$location', function ($scope, $http, $location) {
//your codes
}]);
注入 resolve
的 return 对象,应该注入到控制器中,但事实并非如此。我正在使用一个承诺,以便控制器在承诺得到解决之前不会实例化,因为我正在使用 $http
服务执行 HTTP POST。
我也在使用 $location.path
根据响应数据进行一些重定向。第一条路由将使用它获得的数据重定向到 template1
路由。
$routeProvider
.when("/auth/:id", amd.route(
{
controller: 'eventController',
controllerUrl: 'app/controllers/eventController',
resolve: {
returnedData: ['$http', '$location', '$route', '$q', function ($http, $location, $route, $q) {
var id = $route.current.params.id;
var defer = $q.defer();
$http({
method: 'POST',
url: 'http://domain/service/webapi',
data: { 'name': id }
}).then(function (response) {
var template = "/template".concat(response.data.displayLayout);
defer.resolve(response.data);
$location.path(template);
return defer.promise;
});
}]
}
}))
.when("/template1", amd.route(
{
templateUrl: 'views/templates/template1.html',
controller: 'eventController',
controllerUrl: 'app/controllers/eventController',
}));
app.controller('eventController', ['$scope', '$routeParams', '$timeout', 'returnedData',
function ($scope, $routeParams, $timeout, returnedData) {
// ...controller code here: $scope.returnedData = returnedData
});
我收到 AngularJS 错误:
Error: $injector:unpr Unknown Provider
对此有什么想法吗?
在AngularJSAPI页面,关于routeProvider(https://docs.angularjs.org/api/ngRoute/provider/$routeProvider)
你可以看到:
resolve - {Object.=} ... If any of these dependencies are promises, the router will wait for them all to be resolved or one to be rejected before the controller is instantiated
您应该像下面这样编辑您的代码:
var id = $route.current.params.id;
var defer = $q.defer();
$http({
method: 'POST',
url: 'http://domain/service/webapi',
data: { 'name': id }
}).then(function (response) {
var template = "/template".concat(response.data.displayLayout);
defer.resolve(response.data);
$location.path(template);
});
// return the promise outside `then` method, then angularjs will wait it `resolve`
return defer.promise;
此外,请注意:
Be aware that ngRoute.$routeParams will still refer to the previous route within these resolve functions. Use $route.current.params to access the new route parameters, instead.
使用 ng-route 的依赖注入, 我的解决方案:
var myApp = angular.module("myApp", ["ngRoute", "localytics.directives"]);
myApp.config(['$routeProvider',function ($routeProvider) {
$routeProvider
.when("/", {
templateUrl: "//TimeLine.html",
controller: "MainCtrl"
})
.when("/AddOrEditOccasion", {
templateUrl: "/Html/GiftWhiz/AddOrEditOccasion.html",
controller: "AddOrEditOccasionCtrl"
})
.when("/OccasionStart", {
templateUrl: "/OccasionStart.html",
controller: "OccasionStartCtrl"
})
}]);
myApp.controller("MainCtrl", ['$scope', '$http', '$location', function ($scope, $http, $location) {
//your codes
}]);