How to use this angular factory in controller?

Factory:

factory('cordovaReady', function () { return function (fn) { var queue = []; var impl = function () { queue.push(Array.prototype.slice.call(arguments)); }; document.addEventListener('deviceready', function () { queue.forEach(function (args) { fn.apply(this, args); }); impl = fn; }, false); return function () { return impl.apply(this, arguments); }; }; }) 

I used this factory in another factory as follows:

 return { getCurrentPosition: cordovaReady(function (onSuccess, onError, options) { // } } 

The cordovaReady factory function will complete the passed callback when the deviceReady event was fired. My question is: how can I use it in the controller?

I tried only

 .controller( 'HomeCtrl', function HomeController($scope, cordovaReady) { cordovaReady(function(){ //do stuff }); }); 

But that did not work. No console errors. Any ideas?

+4
source share
4 answers

I solved this by wrapping this factor as follows

 .factory('aUseCase', function ($q, $rootScope, cordovaReady) { return { doSomething: cordovaReady(function () { //do stuff }) }; }) 
+2
source

best version of @artworkad:

 .factory('aUseCase', ['$q', '$rootScope', 'cordovaReady', function ($q, $rootScope, cordovaReady) { return { doSomething: cordovaReady(function () { //do stuff }) }; }]) 

Do not forget to enter dependencies explicitly, otherwise you will have a problem when reducing this fragment.

+2
source

Are you sure your dependency is injected into your controller?

 var MyController = function($scope, cordovaReady) { ... } MyController.$inject = ['$scope', 'cordovaReady']; 
0
source

In the controller you need to declare a function to use cordovaReady

 myApp.controller("salaryCalculatorCtr", ['$scope', 'cordovaReady' , function ($scope, cordovaReady) { var initApp= cordovaReady(function () { //do something }); initApp(); }]); 
0
source

Source: https://habr.com/ru/post/1495294/


All Articles