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

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

问题描述

我是角度ui路由器的新手.

I'm new with angular ui router.

我无法从本地.json文件加载设置.

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

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

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注入到resolve函数中.

Try to inject $rootScope into resolve function first.

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

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