Make change inside app.config() in angularjs

为君一笑 提交于 2019-12-20 05:16:06

问题


I am using googlePlace api in angularJs and I want to change type of places dynamically as needed. Like I use controllers to bind the values in view part using $scope but it's not working in this situation also tried $rootScope.

Tried many other things too but they all are not working and I'm also new to angularJs so don't know much.

Here is code:-

app.config(function(ngGPlacesAPIProvider){
  ngGPlacesAPIProvider.setDefaults({
    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'],
  });
});

Controller code:-

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

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

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

So basically I want to change "types:[]" array from view part.

Any help would be appreciated.


回答1:


You could have those set of default values as an constant, so that you could get those value inside config phase directly, as angular constants are accessible over there & in all other component of angular like controller, factory, directive, etc just by injecting dependency of it.

Constant

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'],
  });
})

Config

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

Whenever you wanted to change the value of ngGPlacesDefaults configuration, you can have handle to those value by injecting ngGPlacesDefaults dependency

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

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



回答2:


I found a different way to solve this problem. You don't need to extra just pass the types:[] as an argument in 'nearBySearch' function

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

"$scope.business.selected.businessType" is bindable dynamically from view, that's it.



来源:https://stackoverflow.com/questions/33579783/make-change-inside-app-config-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!