angularjs包括服务中的另一个模块

时间:2015-10-12 08:23:16

标签: javascript angularjs angular-services

我想在我的服务中包含另一个模块,我有一些常量/配置相关。我的配置模块如下所示:

angular.module('myApp.config', [])
  .constant('EnvironmentConfig', {
    "WindowsAzureADConfig": {
      "tenant": "aaa.onmicrosoft.com", 
      "clientAppId": "xx-xx-xx-xx-xx", 
      "authority": "https://login.windows.net/aa.onmicrosoft.com", 
      "resourceUrl": "https://graph.windows.net/", 
      "redirectUrl": "http://localhost:4400/services/aad/redirectTarget.html"
    }, 
    "AppEndPointConfig": {
      "tokenAccess": "aa-aa-a-a-aa", 
      "baseURL": "http://aa.aaa.aa/api/", 
      "paths": {
        "bpath": "bpath/"
      }
    }, 
    "PConfig": {
      "ApiKey": "aaadd2dasdasdas23f44ffsfsdf", 
      "printerId": 244312
    }
  }
);

我有以下angularjs服务:

(function () {
  function MySrv(EnvironmentConfig) {
    var mySrv = {};
    var app = {
      aFunction: function () {
        console.log(EnvironmentConfig.authority); // getting undefined
      }
    };
    mySrv.app = app;

    return mySrv;
  }

  angular
    .module('myApp') // if adding .module('myApp', ['myApp.config']) then I'm getting a blank screen
    .service('MySrv', mySrv);
})();

请注意,该模块包括在内。在服务本身之前定义。

2 个答案:

答案 0 :(得分:1)

您无法重新声明模块,并且在

时执行此操作
.module('myApp', ['myApp.config'])

正如Gustav所说,你要么包含你第一次声明myApp的myApp.config,要么把你的服务放在自己的模块中,就像你使用myApp.config一样。

.module('myApp.service', ['myApp.config'])

但是,您需要将服务模块包含在myApp模块中。像这样:

.module('myApp', ['myApp.service'])

由于服务模块包含配置模块,因此您无需将其包含在myApp模块中

答案 1 :(得分:0)

权限在对象属性WindowsAzureADConfig中,因此您需要更新日志方法。

在下面的工作代码片段中引用模块依赖注入



angular.module('myApp.config', [])
    .constant('EnvironmentConfig', {"WindowsAzureADConfig":{"tenant":"aaa.onmicrosoft.com","clientAppId":"xx-xx-xx-xx-xx","authority":"https://login.windows.net/aa.onmicrosoft.com","resourceUrl":"https://graph.windows.net/","redirectUrl":"http://localhost:4400/services/aad/redirectTarget.html"},"AppEndPointConfig":{"tokenAccess":"aa-aa-a-a-aa","baseURL":"http://aa.aaa.aa/api/","paths":{"bpath":"bpath/"}},"PConfig":{"ApiKey":"aaadd2dasdasdas23f44ffsfsdf","printerId":244312}});



(function() {

      function MySrv(EnvironmentConfig) {

        var mySrv = {};

        var app = {
        aFunction: function() {
          console.log(EnvironmentConfig.WindowsAzureADConfig.authority); // authority is inside the object property WindowsAzureADConfig, so you need to update the log method
        }
        };

        mySrv.app = app;

        return mySrv;
        }

    angular
    .module('myApp',['myApp.config']) // this is the way to add dependency in module
    .controller('myController', function($scope, MySrv){
        $scope.aFunction = function(){
            MySrv.app.aFunction();
        }
    })
    .service('MySrv',MySrv)
    

    })();

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="myController">
    <a ng-click="aFunction()">Click here</a>
</div>
</body>
&#13;
&#13;
&#13;

希望这有帮助!