javascript – 如何将变量传递给AngularJs配置

我使用
AngularJS Google Maps并需要在模块配置部分配置api_key(类似于
this one的问题).

我的问题是配置AngularJs模块

这是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>

如何从HTML(服务器)端恢复memKey值?

最佳答案 根据您的声明方式,您的memKey位于可访问的范围内.

做就是了

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

snippet

点赞