将http请求中定义的范围变量从控制器传递到指令

时间:2014-12-15 04:37:18

标签: javascript angularjs angularjs-scope

我试图将我的控制器中的get请求的经度和纬度传递给我的指令。但是,正如代码当前所示,在指令中调用的范围变量是“未定义的”。如果我' console.log($ scope.lat);'在get请求之后,在我的控制器中,它也是“未定义的”,因此看起来似乎没有在请求之外传递范围变量。有人知道为什么吗?谢谢

angular.module('Ski').controller('MapCtrl', function($scope, $http) {
  'use strict';

  $http.get('https://quiet-journey-8066.herokuapp.com/mountains/5').success(function(response) {
      $scope.mountain = response.name;
      $scope.lat = response.latitude;
      $scope.lng = response.longitude;

    });

});



angular.module('Ski').directive('mapCanvas', function() {


  return {

    scope: {mapCanvas : '='},
    link: function(scope, element, attrs) {
      var myLatlng = new google.maps.LatLng(scope.$parend.lat, scope.$parent.lng);      
      var mapOptions = {
        center: myLatlng,
        zoom: 14
      };

      var map = new google.maps.Map(element[0], mapOptions);


    }
  };
});

1 个答案:

答案 0 :(得分:1)

Angular Way:

我们希望能够做的是将指令内的范围与外部范围分开,然后将外部范围映射到指令的内部范围。

见这个例子:

angular.module('Ski').directive('mapCanvas', function() {

  return {
    scope: {         
      lat: '=',
      lng: '='
    },
    link: function(scope, element, attrs) {
      var zoom = attrs.zoom || 14;

      var myLatlng = new google.maps.LatLng(scope.lat, scope.lng);

      var mapOptions = {
        center: myLatlng,
        zoom: zoom
      };

      var map = new google.maps.Map(element[0], mapOptions);
    }
  };
});

设置如下:

<div map-canvas lat="lat" lng="lng" zoom="14">
  <!-- your content, zoom is optional-->
</div>

latlng绑定到控制器$scope.lat$scope.lng

的位置

带地图的可运行示例

经验性

angular.module('ski',[])
    
    .controller('ctrl', function($scope) {      
         $scope.lat = 40.71427;
         $scope.lng =  -74.00597;      
         $scope.extra = "New York City";
      
     })
    
    .directive('map', function() {
      return {
        restrict: 'E',
        scope: {
          lat: '=',
          lng: '='
        },
        replace: true,
        template:'<div class="fullscreen"></div>',
        link: function(scope, element, attrs) {
          
          var zoom = parseInt(attrs.zoom) || 14;
          var myLatlng = new google.maps.LatLng(scope.lat, scope.lng);      
          var mapOptions = {
              center: myLatlng,
              zoom: zoom
          };

          var map = new google.maps.Map(element[0], mapOptions);
          
        }
      };
    });
html, body, .fullscreen {
  width: 100%;
  height: 100%;  
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?key=&sensor=false&extension=.js"></script>
<div ng-app="ski" ng-controller="ctrl" class="fullscreen">
<map lat="lat" lng="lng" zoom="12" ></map>
</div>

没有地图的可运行示例

概念。

angular.module('ski',[])
    
    .controller('ctrl', function($scope) {
      
         $scope.lat = -32.634;
         $scope.lng = -54.534;      
         $scope.extra = "Angular Way";
      
     })
    
    .directive('mapCanvas', function() {
      return {
        // creates new isolated scope.
        scope: {
          lat: '=',
          lng: '='
        },
        // here lat, lng, other & extra refers to directive isolated scope, not controller scope.
        template: '<p> Lat: {{lat}} Lng: {{lng}} Other: {{other}} Extra: {{extra}}</p>',
        link: function(scope, element, attrs) {
          // here scope means directive isolated scope.
          var zoom = attrs.zoom || 14;
          var extra = attrs.extra || "no extra";

          scope.other = zoom ;
          scope.extra = extra;
          
          console.log(scope.lat, scope.lng);
        }
      };
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="ski" ng-controller="ctrl">
<div map-canvas lat="lat" lng="lng" zoom="14" extra="{{extra}}">
  <!-- your content, zoom is optional-->
</div>
</div>