2015-03-23 4 views
0

Во-первых, это очень похоже на несколько вопросов, но ни один из ответов не работал для меня.запустите функцию, содержащуюся на заводе извне angularjs

У меня есть приложение Cordova/AngularJS/Ionic, которое каждые 10 минут опробовает удаленный сервер и вытаскивает некоторые JSON - это работает отлично. У меня также есть push-уведомления с плагином PhoneGap Build, снова работающий нормально. То, что я хочу сделать, - это подключить два таких устройства, чтобы при появлении уведомлений он запускал мой опрос, таким образом получая последний контент между периодами опроса.

Существующий код функционирования: обработка уведомлений

var schoolApp = angular.module('schoolApp', ['ionic', 'schoolApp.controllers', 'schoolApp.services']) 

schoolApp.run(function(Poller) {}); 


.factory('Poller', function($http, $interval,updateContentLocalStorage) { 
     var pollerFunct = function() { 
     // fetch and process some JSON from remote server 

     } // END pollerFunct() 




    // run on app startup, then every pollTimeout duration 
    // delay by 10sec so that checkConnectionIsOn() has time to settle on browser platform - seems not needed in actual devices but no harm to delay there too 
    setTimeout(function(){ pollerFunct(); }, 10000); 
    //pollerFunct(); // run on app startup, then every pollTimeout duration 
    $interval(pollerFunct, pollTimeout); 

}) // END factory Poller 

Push, за пределами углового

// handle GCM notifications for Android 
function AndroidOnNotification(e) { 
    // working: call angular service from outside angular: http://stackoverflow.com/questions/15527832/how-can-i-test-an-an-angularjs-service-from-the-console 
    var $http = angular.element(document.body).injector().get('$http'); 
    var $state = angular.element(document.body).injector().get('$state'); 

    // not working : http://stackoverflow.com/questions/26161638/how-to-call-an-angularjs-factory-function-from-outside 
    angular.element(document.body).injector().get('Poller').pollerFunct(); 

} 

Я хочу, чтобы вызвать pollerFunct() из AndroidOnNotification (е), но получить «ProcessMessage не удалось: Ошибка: TypeError: undefined не является функцией "и аналогичные ошибки.

+0

Ваш завод не возвращает ничего, и pollerFunct - это просто локальный var (он не может быть адресован даже с угловым). Вы пробовали что-то вроде Poller: var factory = {}; factory.pollerFunct = function() {...} return factory. –

ответ

0

@ комментарий повторно Пети не вернувшихся ничего привело меня исследовать другие части моего кода, я забыл про и это работает:

.factory('Poller', function() { 
     var pollerFunct = function() { 
     // fetch and process some JSON from remote server 
     } 

    // return something 
    return { 
      poll: function() { 
      pollerFunct(); 
      return; 
      } 
     } 
}) 

Вызывается:

angular.element(document.body).injector().get('Poller').poll(); 
Смежные вопросы