角度模块值未注入

时间:2017-12-12 11:29:18

标签: angularjs

我想通过模块的函数.value存储一些信息。我配置如下

应用

angular.module('MyApp1', ['ngRoute', 'ngCookies', 'ngAnimate', 'angular-loading-bar' ])

angular.module('MyApp2', ['ngRoute', 'ngCookies', 'ngAnimate', 'angular-loading-bar'])

angular.module('MyApp3', ['ngRoute', 'ngCookies', 'ngAnimate', 'angular-loading-bar'])

.value('LoggedUser', {email: '',role: ''})

控制器:

angular.module('MyApp1').controller('LoginController', ['LoggedUser', function ($scope, $cookies, $window, AuthenticationService, StorageService, SessionService, LoggedUser) { }]);

但是我收到关于injection module的错误,AuthenticationServiceStorageServiceSessionService出厂且正常工作。

有什么建议吗?

更新1 了解@JB Nizet说的是什么。修改如下:

angular.module('MyApp1', ['ngRoute', 'ngAnimate', 'angular-loading-bar'])

    .value('LoggedUser', {
        email: '',
        role: ''
    });

angular.module('MyApp2', ['ngRoute', 'ngAnimate', 'angular-loading-bar']);

angular.module('MyApp3', ['MyApp1', 'ngRoute', 'ngAnimate', 'angular-loading-bar'])

依赖关系是否正确?生命周期必须是:

  • 在MyApp1中我在LoggedUser中插入了一些信息。
  • 我在MyApp3中的
  • 是从LoggedUser(来自MyApp1)
  • 读取的
  • MyApp2是独立的,它没有来自MyApp1和MyApp3的任何依赖。

更新2 这天晚上我简化了模块。现在我只有2个模块。模块彼此独立

angular.module('MyApp1', ['ngRoute', 'ngAnimate', 'angular-loading-bar'])

angular.module('MyApp2', ['ngRoute', 'ngAnimate', 'angular-loading-bar'])

我为商店全局变量创建了一个工厂,这个:

angular.module('MyApp2').factory('LoggedUserInformationService', function ($rootScope) {

var LoggedUser = {};

return {
    Set: function (email, isAdmin) {
        LoggedUser.Email = email;
        LoggedUser.IsAdmin = isAdmin;
        return LoggedUser;
    },
    Get: function () {
        return LoggedUser;
    }
};

//var LoggedUser = {};
//var loggedUserService = {};

//loggedUserService.Get = function () {
//    return $rootScope;
//};

//loggedUserService.Set = function (email, battleTag, isAdmin) {
//    User = {};
//    User.Email = email;
//    User.IsAdmin = isAdmin;
//    $rootScope.$emit('LoggedUser', User);

//return loggedUserService;
}).run(function () { });

但是当我在页面中设置数据时,在其他情况下,get是未定义的。

angular.module('MyApp2').controller("MenuController", function ($scope, LoggedUserInformationService) {

$scope.init = function () {
    console.log(LoggedUserInformationService.Get());
}
});

为什么未定义?

2 个答案:

答案 0 :(得分:0)

内联注释注入的依赖项数必须与传入函数的参数计数匹配(隐式注入)。

  

未知提供商:LoggedUserProvider< - LoggedUser< - LoginController

发生上述错误是因为angular无法识别与传入函数的参数相关的LoggedUser(由内联注释方式注入)。

  

使用此类注释时,请注意保留注释   数组与函数声明中的参数同步

angular.module('MyApp1', []);//set injections you need
angular.module('MyApp1').controller( 
    'LoginController'['$scope','$cookies','$window','AuthenticationService','StorageService','SessionService','LoginController', 'LoggedUser',
     function ($scope, $cookies, $window, AuthenticationService, StorageService, SessionService, LoggedUser) {
    //your code
});

此外,你宣布3个模块被分开,并且JB Nizet和maher说你应该有3个子模块和一个主模块,并注入3个模块作为主模块的子模块:

angular.module('MainApp', ['ngRoute', 'ngAnimate', 'angular-loading-bar','MyApp1','MyApp2','MyApp3']);
angular.module('MyApp1', []);
angular.module('MyApp2', []);
angular.module('MyApp3', []);

答案 1 :(得分:0)

您需要在控制器中注入所有依赖项。

angular.module('MyApp1').controller('LoginController', 
  ['$scope', '$cookies', '$window', 'AuthenticationService', 'StorageService', 'SessionService', 'LoggedUser', 
   function ($scope, $cookies, $window, AuthenticationService, StorageService, SessionService, LoggedUser) {
     // function def
    }
  ]
);

或者像这样

angular.module('MyApp1').controller('LoginController', loginCtrl);    

loginCtrl.$inject = ['$scope', '$cookies', '$window', 'AuthenticationService', 'StorageService', 'SessionService', 'LoggedUser'];
function loginCtrl($scope, $cookies, $window, AuthenticationService, StorageService, SessionService, LoggedUser){
  //function def
}
相关问题