"Error: unknown provider" trying to implement dependency injection in Angular.JS

I am new to Angular.JS and I am trying to do dependency injection, but I got this:

// Definition app.factory('Reload', function (load, $timeout, timeout) { if (!timeout) { timeout = 15 * 1000; // reload page every quater minute by default } return $timeout(load, timeout); }); // Controller app.controller('SomeController', function ($scope, $routeParams, $location, $timeout, Installer, Reload) { Reload(load, $timeout, 1000); }); Error: Unknown provider: loadProvider <- load <- Reload at Error (<anonymous>) at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2734:15 at Object.getService [as get] (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2862:39) at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2739:45 at getService (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2862:39) at Object.invoke (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2880:13) at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2740:37 at getService (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2862:39) at invoke (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2880:13) at Object.instantiate (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2914:23) 

What am I missing ?. Thanks

+4
source share
3 answers

The Reload factory definition is mixed between the factory definition and the return function. Update the code as follows.

 // Definition app.factory('Reload', function ($timeout) { return function(load, timeout) { if (!timeout) { timeout = 15 * 1000; // reload page every quater minute by default } return $timeout(load, timeout); } }); // Controller app.controller('SomeController', function ($scope, $routeParams, $location, $timeout, Installer, Reload) { Reload(load, 1000); }); 
+2
source

You pass the load provider to your Reload , which means the load service must be declared in the application.

If you understood correctly, I think you need to add

 app.factory('load', function(){ document.reload(); }); 

before the Reload declaration if you do all this in the same file.

Having said that, if you don't need a particularly complex reboot, I would just take the load out of the injection and include it in $timeout , like

return $timeout(document.reload, timeout);

+1
source

I decided to change the code to:

 // Factory definition app.factory('Reload', function ($timeout) { return function (fnLoad, timeout) { if (!timeout) { timeout = 15 * 1000; // reload page every quater minute by default } return $timeout(fnLoad, timeout); }; }); // Controller app.controller('InstallersController', function ($scope, $routeParams, $location, $timeout, Installer, Reload) { Reload(load); } 

Thanks to the employee ...

+1
source

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


All Articles