为什么我不能设置/获取$ rootScope? [英] Why I can't set/get $rootScope?

查看:116
本文介绍了为什么我不能设置/获取$ rootScope?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在.run()的$ rootScope中设置一个值,并尝试在服务中获取它,但看起来好像是未定义的. .run()和服务中的所有其他程序都运行良好.

I try to set a a value in $rootScope in a .run() and try to get it in a service but it looks like it's undefined. Everything else in the .run() and in the service is running fine.

.run(function($ionicPlatform, $translate, $rootScope) {
  document.addEventListener("deviceready",function(){ onDeviceReady($rootScope); }, false);

  function onDeviceReady($rootScope) {
    if(typeof navigator.globalization !== "undefined") {
      navigator.globalization.getPreferredLanguage(function(language) {
        $rootScope.language='en'; //(language.value).split('-')[0];
        $translate.use((language.value).split("-")[0]).then(function(data) {
          console.log("SUCCESS -> " + data);
        }, function(error) {
          console.log("ERROR -> " + error);
        });
      }, null);
    }
  }
})


.service('Webcache', function ($http, $q, apiHost, $rootScope) {
    return {
        getDataBySection: function (section, version, params) {
            var params = params || {};
            var host = apiHost;
            var deferred = $q.defer();
            var start = new Date().getTime();

            params.lang = $rootScope.language;

            $http.get('http://'+ host +'/api/'+ version +'/'+ section, {
                cache: true,
                params: params
            }).success(function (response) {
                deferred.resolve(response);
            }).error(function (response){
                deferred.resolve(false);
            });
            return deferred.promise;
        }
    };
})

推荐答案

您根本不应该绑定到onDeviceReady事件. Ionic为您提供了一种角度兼容的处理方式:$ionicPlatform.ready()会返回一个承诺.使用它会使您的代码看起来像这样:

You shouldn't be binding to the onDeviceReady event at all. Ionic gives you an angular compatible way to handle it: $ionicPlatform.ready() which returns a promise. Using this would make your code look something like this:

.run(function($ionicPlatform, $translate, $rootScope, $q, $log) {
   $rootScope.languagePromise = $ionicPlatform.ready()
   .then(function() {
        return $q(function(resolve, reject) {
            if(angular.isDefined(navigator.globalization)) {
                navigator.globalization.getPreferredLanguage(function(language) {
                    $translate.use((language.value).split("-")[0]).then(function(data) {
                        $log.debug("SUCCESS -> " + data);
                        resolve(language);
                    }, function(error) {
                        $log.debug("ERROR -> " + error);
                        reject(error);
                    });
                }, null);
            } else { resolve('en'); }
        });
    });

})

这为您带来了希望,您可以随时使用它(立即完成初始化)或在可用时获取语言.

This puts a promise in the scope which you can use at any time to get the language either immediately (if the initialisation is completed), or when it becomes available.

.service('Webcache', function ($http, apiHost, $rootScope) {
    return {
        getDataBySection: function (section, version, params) {
            params = params || {};
            var host = apiHost;
            return $rootScope.languagePromise.then(function(language) {
                params.lang = language;
                return $http.get('http://'+ host +'/api/'+ version +'/'+ section, {
                    cache: true,
                    params: params
                }).catch(function (){
                    return false;
                });
            });
        }
    };
})

直接使用getDataBySection方法返回等待语言承诺的结果,而语言承诺又取决于$http承诺的结果,因此响应数据将像现有代码一样传播回去.

The getDataBySection directly method returns the result of waiting on the language promise which in turn depends on the result of the $http promise, so the response data will propagate back just as for your existing code.

这篇关于为什么我不能设置/获取$ rootScope?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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