【问题标题】:Phonegap / Cordova function in Angular factoryAngular工厂中的Phonegap / Cordova功能
【发布时间】:2014-01-20 22:37:14
【问题描述】:

我正在尝试将 PushPlugin 包装在基于 devgirls post 的 Angular 工厂中,但到目前为止没有成功。

angular.module('phonegap', [])
  .factory('phonegapReady', function ($rootScope, $q) {
    var loadingDeferred = $q.defer();

    document.addEventListener('deviceready', function () {
      $rootScope.$apply(loadingDeferred.resolve);
    });

    return function phonegapReady() {
      return loadingDeferred.promise;
    };
  })
  .factory('push', function ($rootScope, phonegapReady) {
    return {
      registerPush: phonegapReady().then(function (onSuccess, onError) {    

        // stripped handlers

        if (device.platform === 'android' || device.platform === 'Android') {
          pushNotification.register(
            function () {
              var that = this,
                args = arguments;

              if (onSuccess) {
                $rootScope.$apply(function () {
                  onSuccess.apply(that, args);
                });
              }
            },
            function () {
              var that = this,
                args = {
                  'senderID': '123',
                  'ecb': 'onNotificationGCM'
                };

              if (onError) {
                $rootScope.$apply(function () {
                  onError.apply(that, args);
                });
              }
            }
          );
        } else {
          pushNotification.register(
            function () {
              var that = this,
                args = arguments;

              if (onSuccess) {
                $rootScope.$apply(function () {
                  onSuccess.apply(that, args);
                });
              }
            },
            function () {
              var that = this,
                args = {
                  'badge': 'true',
                  'sound': 'true',
                  'alert': 'true',
                  'ecb': 'onNotificationAPN'
                };

              if (onError) {
                $rootScope.$apply(function () {
                  onError.apply(that, args);
                });
              }
            }
          );
        }
      })
    };
  });

得到一个错误:

TypeError: '[object Object]' is not a function (evaluating 'e.registerPush(function(a){console.log("fun"),console.log(a)})')

我做错了什么?

【问题讨论】:

    标签: angularjs cordova phonegap-plugins


    【解决方案1】:

    当您在承诺上调用 then 时,它会返回承诺,以便您可以链接回调。

    我认为用函数包装 registerPush 会起作用,例如:

    registerPush: function(onSuccess, onError) {
        phonegapReady().then(function () {   
            // Do something with closured onSuccess and onError
        });
    },..
    

    【讨论】:

      猜你喜欢
      • 2013-11-27
      • 2023-03-16
      • 1970-01-01
      • 2015-02-16
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      相关资源
      最近更新 更多