2016-03-16 9 views
1

我收到一個谷歌地圖的地方ID從休息API。然後我使用谷歌地圖API來獲取地方對象是這樣的:ng模型只更新輸入後點擊時使用谷歌地圖自動完成

var geocoder = new google.maps.Geocoder; 
     geocoder.geocode({ 
      'placeId': data.city 
     }, function(results, status) { 
      if (status === google.maps.GeocoderStatus.OK) { 
       if (results[0]) { 
        $scope.place = results[0]; 
       } else { 
        window.alert('No results found'); 
       } 
      } else { 
       window.alert('Geocoder failed due to: ' + status); 
      } 
     }); 

我寫我正在使用的前端這樣它的作用域變量中的結果:

<label><span>Stadt</span><input type="text" g-places-autocomplete ng-model="place.formatted_address" name="city" class="f-trans"/></label> 

我我正在使用github的kuhnza/angular-google-places-autocomplete作爲自動完成功能。不知怎的,現在我有問題了,當範圍變量被頂部的函數設置時,它不會更新輸入。只有當我在輸入字段內部單擊並且再次在輸入字段外部時,它纔會被更新,並且自動完成功能正在運行而沒有任何問題。它不需要使用我使用的「kuhnza/angular-google-places-autocomplete」標籤,因爲如果我刪除它,它仍然不起作用。

回答

1

您正在更新angular以外的模型。所以它沒有意識到價值已經更新,直到你與angular交互。最後,我檢查了最簡單的方法,以確保更新是在角度摘要中包裝它在$timeout(確保你也注入了它)。

if (results[0]) { 
    $timeout(function() { 
    $scope.place = results[0]; 
    }); 
} else { 
    window.alert('No results found'); 
} 
相關問題