Angularjs access rootscope in resolve state

2019-06-02 11:19发布

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条回答
萌系小妹纸
2楼-- · 2019-06-02 11:44
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.

查看更多
登录 后发表回答