简体   繁体   English

AngularJS全局变量无法在模块中访问

[英]AngularJS global variable not accessible in a module

In my AngularJS project I've got a global variable that contains some data: the domain name and image directory. 在我的AngularJS项目中,我有一个包含一些数据的全局变量:域名和图像目录。 I need this variable in some views. 在某些视图中,我需要此变量。 Here is how the variable looks like and how it is set: 这是变量的外观以及设置方式:

config.js config.js

(function(){
  'use strict';
   var conf = angular.module('stormforsStats.config', ['ngRoute']);

   //SET GENERAL VARIABLES
   conf.value('SERVER_DATA', {
     'BASE_URL': '/',
     'IMG_URL': 'assets/src',
     'API_URL': '../api/'
   });
   conf.value('APP_DATA', {
     'APP_NAME': 'Stormfors Stats',
     'APP_VERSION': '0.1'
   });

   conf.config(['$routeProvider', function($routeProvider) {
      $routeProvider.when('/', {
          templateUrl: 'index.html',
          controller: 'ConfCtrl'
      }).otherwise({
          redirectTo: '/'
      });
   }]);

   conf.controller('ConfCtrl', ['$log', '$location', 'SERVER_DATA', function ConfCtrl($log, $location, SERVER_DATA) {
        SERVER_DATA.BASE_URL = $location.absUrl().substring(0, $location.absUrl().length - $location.url().length).replace("#","");
        SERVER_DATA.IMG_URL = SERVER_DATA.BASE_URL + SERVER_DATA.IMG_URL;
   }]);
})();

I already successfully injected the global variable, into one of my other modules like this: header.js 我已经成功将全局变量注入到其他模块之一中,例如: header.js

(function(){
  'use strict';
  var header = angular.module('stormforsStats.header', []);

  header.directive("headerBar", [function(){
    return {
      restrict: "E",
      templateUrl: "header/header.html",
      controller: 'HeaderCtrl',
      controllerAs: 'header'
    };
  }]);

  header.controller('HeaderCtrl', ['$log', '$scope', '$location', 'SERVER_DATA', function($log, $scope, $location, SERVER_DATA){
    $log.log('header controller initialized');
    this.imgUrl = SERVER_DATA.IMG_URL;
    this.baseUrl = SERVER_DATA.BASE_URL;
  }]);
})();

In this module I successfully access the variable in the module's template. 在此模块中,我成功访问了模块模板中的变量。 When I try this in another template, notifications, this also works: notifications.js 当我在另一个模板(通知)中尝试此操作时,此方法也起作用: notifications.js

(function(){
  'use strict';

  var notifications = angular.module('stormforsStats.notifications', ['ngRoute'])

  notifications.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/notifications', {
      templateUrl: 'parts/pages/notifications/notifications.html',
      controller: 'NotificationsCtrl'
    });
  }])

  notifications.controller('NotificationsCtrl', ['$log', 'SERVER_DATA', function($log, SERVER_DATA) {
    this.imgUrl = SERVER_DATA.IMG_URL;
    this.baseUrl = SERVER_DATA.BASE_URL;
    $log.log('notifications controller: '+this.baseUrl);
    $log.log(SERVER_DATA);
  }]);
})();

The code above successfully logs the variable. 上面的代码成功记录了变量。

No problem so far, but when I try to inject the variable into a very similar module, it doesn't work. 到目前为止,还没有问题,但是当我尝试将变量注入到非常相似的模块中时,它将无法正常工作。 Even when I type a typo in the injections, it doesn't get detected, although the controller is executed and the log in the controller shows: home.js 即使我在进样中输入错字,也不会被检测到,尽管控制器已执行且控制器中的日志显示: home.js

(function(){
  'use strict';

  var home = angular.module('stormforsStats.home',['ngRoute', 'stormforsStats.factory'])

  home.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/', {
      templateUrl: 'home/home.html',
      controller: 'HomeCtrl',
      controllerAs: 'home'
    });
  }])

  home.controller('HomeCtrl', ['$rootScope', '$scope', '$log', 'dataFactory', 'SERVER_DATA', function($rootScope, $scope, $log, dataFactory, SERVER_DATA) {
    this.imgUrl = SERVER_DATA.IMG_URL;
    this.baseUrl = SERVER_DATA.BASE_URL;
    $log.log('home controller: '+this.baseUrl);
    $log.log(SERVER_DATA);
  }]);
})();

The home.js code does log 'home controller' but nothing about the injected variable. home.js代码确实记录了“ home controller”,但与注入的变量无关。 Could anyone help me with this problem, or put me in the right direction? 有人可以帮助我解决这个问题,还是让我朝正确的方向发展? Thanks in advance! 提前致谢!

var home = angular.module('stormforsStats.home')

initiates a different module than the module that you initiate with 启动与您启动的模块不同的模块

var conf = angular.module('stormforsStats.conf').

You also didn't add stormforStats.conf to the dependencies of stormforsStats.home so you can't use something that your module doesn't recognises. 您也没有将stormforStats.conf添加到stormforsStats.home的依赖项中,因此您不能使用模块无法识别的内容。 Add the dependency and it'll be solved ( this should solve it: var home = angular.module('stormforsStats.home',['ngRoute', 'stormforsStats.factory', 'stormforsStats.config']) ) 添加依赖关系,它将解决(这应该解决它: var home = angular.module('stormforsStats.home',['ngRoute', 'stormforsStats.factory', 'stormforsStats.config']) ))

当您的stormforsStats.home模块依赖于stormforsStats.config模块时,然后在模块声明中将其枚举

var home = angular.module('stormforsStats.home',['ngRoute', 'stormforsStats.factory', 'stormforsStats.config'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM