简体   繁体   中英

How to pass the variables to AngularJs config

I work with AngularJS Google Maps and need to configure the api_key in the module config part (question similar to this one ).

My question is about configuring AngularJs modules

This is my test pen :

 var app = angular.module('myapp', []) /* // HERE ????? // I try to pass a value from the HTML (server side) .config(function($window){ console.log($window.memKey); }) */ .controller('MainCtrl', ['$scope', '$window', function($scope, $window) { $scope.name = $window.memName; $scope.city = $window.memCity; $scope.getMember = function(id) { console.log(id); }; }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script type="text/javascript"> var memKey = 'bb7de28f-0f89-4f14-8575-d494203acec7'; var memName = 'John'; var memCity = 'New-York'; </script> <div ng-app="myapp" ng-controller="MainCtrl"> Member name: {{name}} <br> Member city: {{city}} </div> 

How is possible to recuperate the memKey value from the HTML (Server) side?

your memKey is in an accesible scope, according on how you've declared it.

just do

 app.config(function(){
    console.log(memKey);
  }) 

See snippet

You can access an existing angular module if you don't use the injection array. After you have the reference, you can declare constants eg:

<script type="text/javascript">
 var app = angular.module('myapp');  
 app.constant('memKey', 'bb7de28f-0f89-4f14-8575-d494203acec7');
 app.constant('memName', 'John');
 app.constant('memCity', 'New-York');
 //Or do them as one config object
</script>   

--

  .config(['memKey', 'memName','memCity'  function(memKey, memName, memCity) {
  }]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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