如何从另一个控制器中的resolve获取数据?

时间:2015-10-03 13:36:21

标签: angularjs

我使用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);

我在控制台

中收到未定义的消息

0 个答案:

没有答案