2016-01-13 89 views
0

我跟随This fiddle在我的离子应用程序中实现相同。

这里是我的指令:

angular.module('mobApp.services'). 
      directive('googlePlaces', function(){ 
       return { 
        restrict:'E', 
        replace:true, 
        // transclude:true, 
        scope: {location:'='}, 
        template: '<input id="google_places_ac" name="google_places_ac" type="text" class="input-block-level"/>', 
        link: function($scope, elm, attrs){ 
         var autocomplete = new google.maps.places.Autocomplete($("#google_places_ac")[0], {}); 
         google.maps.event.addListener(autocomplete, 'place_changed', function() { 
          var place = autocomplete.getPlace(); 
          $scope.location = place.geometry.location.lat() + ',' + place.geometry.location.lng(); 
          $scope.$apply(); 
         }); 
        } 
       } 
      }); 

在这里,我在控制器中使用它:

angular.module("mobApp.controllers") 
.controller("AutocompleteTextboxController",function($scope) { 

    $scope.location = ''; 
    $scope.selectionDone = function() { 
     alert('Yay. Location: ' + $scope.location); 
    } 
}); 

我在这里呼吁该指令

<ion-view view-title="{{title}}"> 
<ion-nav-bar class="bar-positive">   
    <ion-nav-back-button></ion-nav-back-button> 
    <ion-nav-buttons side="right"> 
     <button class="button button-icon icon ion-android-done" ng-disabled="selectedItem.id==''" ng-click="selectionDone()"></button> 
    </ion-nav-buttons>  
</ion-nav-bar> 
    <ion-content> 
    <div class="list"> 
     <ion-list ng-show="selectionType == 'place'"> 
     <google-places location=location></google-places> 
     </ion-list> 
    </div>  
    </ion-content> 
</ion-view> 

回答

0

尝试更换:

$scope 

scope 

在你的指令。 $表示您注入的范围实际上不在链接函数内。对此的一个很好的解释是here