AngularJS injecting third party module ngIdle
Asked Answered
C

1

6

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() {}
    }());
Compressive answered 7/12, 2018 at 14:34 Comment(2)
make sure to add the modules of curUser, userAccount in angular.module('Demo', ['ngIdle','curUserModuleName', 'userAccountModuleName''])Usher
@RameezRaja adding angular.module('Demo', ['ngIdle','curUser', 'userAccount']) did not fix the problem now giving Error: [$injector:modulerr] Failed to instantiate module curUser due to: Error: [$injector:nomod] Module 'curUser' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.Compressive
S
1

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.

Sext answered 13/12, 2018 at 0:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.