【问题标题】:Angular $rootScope init. ( Angular + Phonegap )角度 $rootScope 初始化。 (角度+电话间隙)
【发布时间】:2015-07-21 17:27:03
【问题描述】:

尝试使用android在phonegap中创建android应用。

angular.module('myModule').config(['$rootScope', function($rootScope) {
    $rootScope.internet = { status: 0, type: 'none'};
    document.addEventListener("offline", function(){
        $rootScope.internet.status = 0;
        $rootScope.internet.type = navigator.connection.type;
    }, false);
    document.addEventListener("online", function(){
        $rootScope.internet.status = 1;
        $rootScope.internet.type = navigator.connection.type;
    }, false);
  }]);

我只想在 navigator.connection 更改时更改全局变量,或者在线/离线。

或者我如何将watch 绑定到这个navigator.connection 全局变量。

【问题讨论】:

    标签: javascript android angularjs cordova phonegap-plugins


    【解决方案1】:

    您不能在 Angular 应用程序的配置阶段使用 $rootScope,因为它确实是在 run 块中创建的,该块在 .config 块结束后立即运行。

    它确实可以从角度run 阶段获得。你可以把这段代码放在运行阶段。

    您可以使用函数并在每个摘要上返回 navigator.connection 值。

    代码

    angular.module('myModule').run(['$rootScope', function($rootScope) {
        $rootScope.internet = { status: 0, type: 'none'};
        document.addEventListener("offline", function(){
            $rootScope.internet.status = 0;
            $rootScope.internet.type = navigator.connection.type;
        }, false);
        document.addEventListener("online", function(){
            $rootScope.internet.status = 1;
            $rootScope.internet.type = navigator.connection.type;
        }, false);
        $rootScope.$watch(function(){
           return navigator.connection;
        }, function((newVal, oldVal){
           console.log(newVal)
        })
    }]);
    

    【讨论】:

    • @ubombi 对你有帮助吗?
    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 2015-08-19
    • 2020-08-23
    • 1970-01-01
    • 2015-04-05
    • 2016-03-07
    • 2016-09-22
    • 1970-01-01
    相关资源
    最近更新 更多