2017-01-10 56 views
3

我使用angularjs版本1.13.X.In我的應用程序有一個谷歌地方自動完成搜索框作爲指令。當加載頁面錯誤顯示在控制檯中,但搜索框工作正常。谷歌地方自動完成顯示錯誤控制檯在angularjs?

InvalidValueError: setComponentRestrictions: in property country: not a string; and not an Array 

我的指令代碼

routerApp.directive('googleplace', ['$state', function ($state) { 
      return { 
       require: 'ngModel', 
       link: function (scope, element, attrs, model) { 
        var options = { 
         types: [], 
         componentRestrictions: {} 
         }; 
        scope.gPlace = new google.maps.places.Autocomplete(element[0], options); 
        google.maps.event.addListener(scope.gPlace, 'place_changed', function (e) { 
         scope.$apply(function() { 
          model.$setViewValue(element.val()); 
          var countrycode = "na"; 
          var latitude = 0; 
          var longitude = 0; 
          scope.secondplacebox = scope.chosenPlace; 
          for (var i = 0; i < scope.gPlace.getPlace().address_components.length; i++) { 
           if (scope.gPlace.getPlace().address_components[i].types[0] == 'country') { 
            countrycode = scope.gPlace.getPlace().address_components[i].short_name; 
           } 
          } 
          latitude = scope.gPlace.getPlace().geometry.location.lat().toFixed(6); 
          longitude = scope.gPlace.getPlace().geometry.location.lng().toFixed(6); 
          //$state.go('home'); 
          scope.Fn_setCookieData(latitude, longitude, countrycode); 

         }); 
        }); 
       } 
      }; 
     } 
    ]); 

如何解決這個問題?請幫助我,並提前感謝

回答

10

我看到optionscomponentRestrictions並且它不限於特定的國家。像componentRestrictions: {country: 'fr'}這樣的國家/地區限制可以解決問題。將fr替換爲您的國家/地區要求。如果你不需要限制一個國家,只是避免在選項中使用它。

希望這可以幫助你!

+3

感謝它的工作,我刪除了componentRestrictions:{} –

+0

這是行得通的。謝謝 – Moppo

+0

錯誤消息應該更清楚 –

2

我有同樣的問題,但我已經更改爲版本3,它已經工作。

Before http://maps.googleapis.com/maps/api/js?key=[MyKey]&libraries=places&language=en 

After http://maps.googleapis.com/maps/api/js?key=[MyKey]&libraries=places&language=en&v=3 
相關問題