6

我有一个控制器,如下所示。

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


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

我尝试包含一个名为 ngIdle 的第三方模块,生成的代码如下所示。

(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();
    });

}());

现在我收到一个错误错误:[$injector:unpr] Unknown provider: curUserProvider <- curUser <- MainController。这是 curUser 工厂定义

    (function () {
    "use strict";
        angular.module("services").factory("curUser", curUser)
           function curUser() {}
    }());
4

1 回答 1

1

curUserservices模块中定义,因此需要将其作为依赖项添加到您的应用程序中。甚至在添加ngIdle.

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

工厂函数也应该返回一些东西。

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

请参阅工作plunker

于 2018-12-13T00:57:32.477 回答