2015-11-07 53 views
1

我在angularJs中使用googlePlace api,並且想根據需要動態更改地點類型。就像我使用控制器綁定視圖部分中的值使用$範圍,但它不工作在這種情況下也嘗試$ rootScope。在angularjs中對app.config()進行更改

嘗試了很多其他的東西,但他們都沒有工作,我也是新的angularJs所以不知道很多。

這裏是代碼: -

app.config(function(ngGPlacesAPIProvider){ 
 
    ngGPlacesAPIProvider.setDefaults({ 
 
    radius:1000000, 
 
\t types:['electronics_store','bakery','bank','beauty_salon','bicycle_store','book_store','cafe','car_dealer','car_wash','car_repair','clothing_store','dentist','department_store'], 
 
\t nearbySearchKeys: ['name','geometry', 'reference'], 
 
\t placeDetailsKeys: ['name','formatted_address', 'formatted_phone_number', 
 
     'reference', 'website', 'geometry', 'email'], 
 
    }); 
 
});

控制器代碼: -

app.config(function(ngGPlacesAPIProvider, ngGPlacesDefaults){ 
 
\t ngGPlacesAPIProvider.setDefaults(ngGPlacesDefaults); 
 
\t }); 
 

 
app.controller('scdfindCustomerCtrl',function($scope,ngGPlacesAPI,$http,ngGPlacesDefaults){ 
 

 
\t ngGPlacesDefaults.types = ["atm"]; 
 
\t 
 
\t $scope.getDetails = function(ref){ 
 
\t \t $scope.details = ngGPlacesAPI.placeDetails({reference:ref}).then(
 
\t \t \t  function (data) { 
 
\t \t \t  \t $scope.det=data; 
 
\t \t \t  console.log(data); 
 
\t \t \t  return data; 
 
\t \t \t  }); 
 
\t } 
 
\t 
 
\t $scope.positions = [{lat:37.7699298,lng:-122.4469157}]; 
 
\t 
 
\t $scope.addMarker = function(event) { 
 
    var ll = event.latLng; 
 
\t \t 
 
    $scope.positions.push({lat:ll.lat(), lng: ll.lng()}); 
 
    
 
\t 
 
\t $scope.data = ngGPlacesAPI.nearbySearch({latitude:ll.lat(), longitude:ll.lng()}).then(
 
    function(data){ 
 
\t \t $scope.person=data; 
 
\t \t console.log(data); 
 
\t return data; 
 
    }); 
 
}

所以基本上我想改變「類型:[] 「從視圖部分數組。

任何幫助,將不勝感激。

回答

2

你可以有這些組默認值作爲一個常數,這樣你可以直接獲取配置階段內的值,如角constant s爲在角像controller所有其它部件那邊&訪問,factorydirective,等注入它的依賴。

app.constant('ngGPlacesDefaults', { 
    radius:1000000, 
    types:['electronics_store','bakery','bank','beauty_salon','bicycle_store','book_store','cafe','car_dealer','car_wash','car_repair','clothing_store','dentist','department_store'], 
    nearbySearchKeys: ['name','geometry', 'reference'], 
    placeDetailsKeys: ['name','formatted_address', 'formatted_phone_number', 
     'reference', 'website', 'geometry', 'email'], 
    }); 
}) 

配置

app.config(function(ngGPlacesAPIProvider, ngGPlacesDefaults){ 
    ngGPlacesAPIProvider.setDefaults(ngGPlacesDefaults); 
}); 

每當你想改變配置的值,可以通過注入依賴有手柄,那些價值

app.controller('myController', function($scope, ngGPlacesDefaults){ 
    //other code here 

    ngGPlacesDefaults.types = ["some", "different", "values"]; //you could change value like this 
}) 
+0

好的,謝謝你的朋友 – Anonymous

+0

@Anonymous我沒有給你.. –

+0

我在我的app.js文件中添加了「常量部分」,並將「types:[] array」留空。 現在我想在我的控制器中設置類型數組的值讓我們說類型:「atm」,但它不顯示atms列表。 – Anonymous

0

我找到了解決此問題的不同方式。你不需要額外的只是通過類型:[]在「nearBySearch」功能參數

$scope.data = ngGPlacesAPI.nearbySearch({latitude:ll.lat(), longitude:ll.lng(), types:[$scope.business.selected.businessType]}).then(
 
    function(data){ 
 
\t \t $scope.person=data; 
 
\t \t console.log(data); 
 
\t return data; 
 
    });

「$ scope.business.selected.businessType」是綁定動態地從視圖, 而已。