$ionicPlatform.ready() 与 AngularJS Cordova 中的控制器之间的绑定数据

Binding data between $ionicPlatform.ready() & a controller in AngularJS Cordova

我像这样在 app.ready() 中使用了 Push Plugin https://github.com/phonegap-build/PushPlugin

window.onNotification = function(e) {
    switch( e.event ) {
        case 'registered':
            if ( e.regid.length > 0 ) {
                // Your GCM push server needs to know the regID before it can push to this device
                // here is where you might want to send it the regID for later use.

                window.localStorage.setItem('regid',e.regid);
            }
            break;
        case 'message':
            if (e.foreground) {
                //HI, HERE I GET THE NOTIFICATION MESSAGE e.payload.message
            }
    }
}

我有一个控制器,它在 html 中显示消息:

.controller("msjctrl", function() {
    this.msjs=['message1','message2'];
}

和html

<ion-content id="msjcontent" ng-controller="msjctrl as mm">
    <h1 class="button-positive" id="log">Mensajes</h1>
    <div class='card' ng-repeat="msj in mm.msjs">
        {{msj}}
    </div>
</ion-content>

如何将从 app.ready() 获取的消息推送到控制器的 msjs 数组(绑定方式)中?

如果您的 app.ready() 方法可以访问 Angular 的 $rootScope,也许您可​​以广播更改。这是未经测试的,但是是这样的:

// inside the `message` case statement
if (e.foreground) {
    $rootScope.$broadcast('$newMessageReceived', e.payload.message);
}

然后,在您的控制器中观看广播:

.controller('msjctrl', function($scope) {
    var self = this;

    this.msjs=['message1','message2'];

    $scope.$on('$newMessageReceived', function(e, msg) {
        console.log('message received:', msg);
        self.msjs.push(msg);
    });
})

Here is a jsfiddle example.