2016-02-12 67 views
0

因此,我有一個輸入字段,它從Google自動填充api中選擇一個地址。ng-model將輸入字段中的文本更改爲[Object object]

<div class="form-group" ng-show="editing || creating" has-class="{'has-error': roomForm.address.$invalid && (roomForm.address.$touched || roomForm.$submitted)}"> 
       <input address-search selected-address="selectedAddress" class="form-control" placeholder="Where is your place located?" name="address" ng-model="room.address" required type="text"> 
       <div class="alert alert-danger" ng-show="roomForm.address.$error.required && (roomForm.address.$touched || roomForm.$submitted)" role="alert">The room location is required</div> 
      </div> 

這是附加的自動完成到輸入指令

angular.module('app') 
    .directive('addressSearch', function() { 
    return { 
     scope: { 
     addressSearch: '=', 
     selectedAddress: '=selectedAddress' 
     }, 
     link: function(scope, element, attrs) { 
     var options = { 
      types: ['address'] 
     }; 

     var autocomplete = new google.maps.places.Autocomplete(element[0], options); 

     autocomplete.addListener('place_changed', function() { 
      var address = autocomplete.getPlace(); 
      scope.selectedAddress(address); 
     }); 
     } 
    }; 
    }); 

的問題是,當我選擇一個地址,然後在另一種形式中元件在輸入字段開始鍵入,該地址將改爲[Object object]而不是顯示我最初選擇的地址。我知道它正在顯示,因爲它試圖將ng模型顯示爲一個字符串,但我該如何阻止它呢?

回答

1

selectedAddress是一個對象。您需要訪問該對象上的屬性才能獲取要查找的字符串。

扔在那裏console.log(selectedAddress)看到物體是什麼樣子,然後找到你所需要的性能和使用上ng-model,像ng-model="room.address.foo.bar"

相關問題