Angularjs access rootscope in resolve state

元气小坏坏 提交于 2019-12-08 04:13:21

问题


I'm new with angular ui router.

I have problems with loading settings from locally .json file.

I save the settings on app.run() in $rootscope.settings.

.run(['$rootScope', '$http', '$state', '$stateParams', function ($rootScope, $http, $state, $stateParams) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;

    $http.get('./App/Core/Config/config.json').success(function (data) {
        $rootScope.settings = data;
    });

}])

before I go to $state I need a setting from $rootscope.settings to get data. But in the resolve the $rootscope.settings is underfined.

.state('alertcenter', {
            url: '',
            views: {
                'alertcenter': {
                    templateUrl: './App/Features/AlertCenter.html',
                    controller: 'AlertCenter'
                }
            },
            resolve: {
                Data: ['$http', function ($http) {
                    return $http({
                        method: 'GET',
                        url: $rootScope.settings.baseUrl + '/getData',
                        withCredentials: true
                    });
                }]
            }
        });

How can i access the settings in resolve of $state? Or is there another better way?


回答1:


resolve: {
                Data: ['$http','$rootScope', function ($http, $rootScope) {
                    return $http({
                        method: 'GET',
                        url: $rootScope.settings.baseUrl + '/getData',
                        withCredentials: true
                    });
                }]
            }

Try to inject $rootScope into resolve function first.



来源:https://stackoverflow.com/questions/28165337/angularjs-access-rootscope-in-resolve-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!