触发phonegap deviceready事件后设置$ rootScope变量

时间:2013-11-25 22:26:07

标签: angularjs cordova

在触发deviceready事件后,有没有办法在我的角应用中设置$ rootScope变量?

var application = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},

// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, true);
},
// deviceready Event Handler
onDeviceReady: function() {
    //set angular app $rootScope variable
}

};

1 个答案:

答案 0 :(得分:3)

要在 deviceready 事件处理程序中的 $ rootScope 中设置属性,请确保将其包装在$ apply方法调用中,因为 deviceready < / strong>事件发生在AngularJS之外。这是一个例子:

angular.module('myapp').run(['$rootScope', function($rootScope) {
    document.addEventListener('deviceready', function() {
        $rootScope.$apply(function() {
            $rootScope.myVariable = "variable value";
        });
    });
});