Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS injecting third party module ngIdle

I have a controller as shown below.

(function () {
"use strict";
var app = angular.module('Demo')
    .controller('MainController', ['$rootScope', '$scope', '$location', 'curUser',MainController]);


 function MainController($rootScope, $scope,  $location, curUser) {
       //some logic here  
 }());

I tried to include a third party module called ngIdle and the resultant code looks like below.

(function () {
"use strict";
var app = angular.module('Demo', ['ngIdle'])
    .controller('MainController', ['$rootScope', '$scope', '$location', 'curUser','Idle', function ($rootScope, $scope, $location, curUser, Idle) {


    }]).config(function (IdleProvider, KeepaliveProvider) {
        // configure Idle settings
        IdleProvider.idle(5); // in seconds
        IdleProvider.timeout(5); // in seconds
        KeepaliveProvider.interval(2); // in seconds
    })
    .run(function (Idle) {
        // start watching when the app runs. also starts the Keepalive service by default.
        Idle.watch();
    });

}());

Now I get an error as Error: [$injector:unpr] Unknown provider: curUserProvider <- curUser <- MainController. Here's the curUser factory definition

    (function () {
    "use strict";
        angular.module("services").factory("curUser", curUser)
           function curUser() {}
    }());
like image 803
TheLoneWolf91193 Avatar asked Dec 07 '18 14:12

TheLoneWolf91193


1 Answers

curUser is defined in the services module so it needs to be added as a dependency in your app. This would have been the case even before adding ngIdle.

var app = angular.module('Demo', ["ngIdle", "services"])

The factory function should also return something.

function curUser() {
  return { username: 'fooBar'};
}

See working plunker.

like image 110
logee Avatar answered Sep 18 '22 12:09

logee