chachan
chachan

Reputation: 2462

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

I'm brand new in Angular.JS and I'm trying to make a 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

Upvotes: 4

Views: 5197

Answers (3)

chachan
chachan

Reputation: 2462

I solved changing 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 a co-worker...

Upvotes: 1

Buu
Buu

Reputation: 50395

The definition of the Reload factory gets mixed up between the factory definition and the function it returns. Update your 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);
});

Upvotes: 2

Joe Minichino
Joe Minichino

Reputation: 2773

You're passing a load provider into your Reload, that means a service load needs to be declared in the app.

if I understand you correctly i think you need to add

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

before your Reload declaration, if you're doing all that in the same file that is.

Having said that, unless you need a particularly complex reloading I would simply take out load from the injection and include it in the $timeout like

return $timeout(document.reload, timeout);

Upvotes: 1

Related Questions