Angularjs 在解析状态下访问 rootscope [英] Angularjs access rootscope in resolve state

查看:33
本文介绍了Angularjs 在解析状态下访问 rootscope的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 angular ui 路由器的新手.

I'm new with angular ui router.

我在从本地 .json 文件加载设置时遇到问题.

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

我将 app.run() 上的设置保存在 $rootscope.settings 中.

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;
    });

}])

在我去 $state 之前,我需要 $rootscope.settings 中的一个设置来获取数据.但是在解析中 $rootscope.settings 是不够完善的.

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
                    });
                }]
            }
        });

如何访问 $state 解析中的设置?或者有其他更好的方法吗?

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

推荐答案

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

先尝试将 $rootScope 注入到解析函数中.

Try to inject $rootScope into resolve function first.

这篇关于Angularjs 在解析状态下访问 rootscope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆