Grunt环境变量未及时加载

时间:2017-10-02 18:55:50

标签: javascript angularjs gruntjs

我正在使用grunt-ng-constant将环境变量加载到我的角度应用程序中。除了加载应用程序之外,这似乎正常工作95%。例如,当我加载应用程序时,我无法访问变量,因此如果我记录ENV基础对象,它将显示为未定义。如果我导航到下一页,我可以登录ENV并获得对我的变量的访问权限。在我看来,应用程序在加载config.js之前加载,我不确定如何更改它。

Generating the config.js file

的index.html     

<script src="scripts/app.js"></script>
<script src="scripts/config.js"></script>
 ....

app.js

    angular
    .module('serverSideConfiguratorApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'angularSpinner',
    'smart-table',
     'config'
  ])

gruntfile

 ngconstant: {
    qa: {
                  options: {
                      dest: 'app/scripts/config.js'
                  },
                  constants: {
                      ENV: {
                          name: 'qa',
                          server: [{
                              "name": "QA1 (232)",
                              "url": "http://xxxxx/"
                          }, {
                              "name": "QA2 (233)",
                              "url": "http://xxxxx/"
                          }, {
                              "name": "QA3 (LB)",
                              "url": "http://xxxxx//"
                          }]
                      }
                  }
              }
}

我已经尝试在我的index.html中首先创建config.js,但这没有任何影响。如果有人能提供任何见解,将不胜感激!此外,如果您需要更多代码或有任何问题,请告诉我。

由于

1 个答案:

答案 0 :(得分:0)

怀疑答案很简单(而且很烦人)。我不知道对控制器的注射顺序很重要所以我有这个:

angular.module('serverSideConfiguratorApp')
        .controller('ModelsCtrl', [ '$scope', '$http', 'BaseURLService', 'configuratorService', '$q', '$timeout','$route', 'ENV',
                    function( $scope, $http, BaseURLService, configuratorService, $q, $timeout, ENV)

更改为此后(首先注入ENV):

angular.module('serverSideConfiguratorApp')
        .controller('ModelsCtrl', ['ENV', '$scope', '$http', 'BaseURLService', 'configuratorService', '$q', '$timeout','$route',
                    function(ENV, $scope, $http, BaseURLService, configuratorService, $q, $timeout)
现在一切正常!