我使用resolve
函数在Angular JS中使用路由:
.when("/:type/:user/:id", {
template: " ",
controller: "AppController",
resolve: {
"myVar": function (appService) {
return appService.get({type : "5"}).then(function (response) {
return response.app;
});
}
}
})
在控制器AppController
我有:
.controller('AppController', ['$scope', '$http', '$route', '$sce', 'appService', 'myVar', function ($scope, $http, $route, $sce, appService, myVar) {
$scope.app = myVar;
}])
我还有另一个控制器:
.controller('SecondController', ['$scope', '$http', '$route', '$sce', 'appService', function ($scope, $http, $route, $sce, appService) {
// Get here $scope.app from first controller
}])
我使用service
:
.controller('AppController', ['$scope', '$http', '$route', '$sce', 'appService', 'myVar', function ($scope, $http, $route, $sce, appService, myVar) {
appService._set(myVar);
}])
.controller('SecondController', ['$scope', '$http', '$route', '$sce', 'appService', function ($scope, $http, $route, $sce, appService) {
$scope.app = appService._get();
}])
当我尝试通过服务获取控制器SecondController
中的数据时:
$scope.app = appService._get();
console.log($scope.app);
我在控制台
中收到未定义的消息