Make change inside app.config() in angularjs

前端 未结 2 1570
情书的邮戳
情书的邮戳 2021-01-23 10:39

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

相关标签:
2条回答
  • 2021-01-23 11:20

    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.

    0 讨论(0)
  • 2021-01-23 11:38

    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
    })
    
    0 讨论(0)
提交回复
热议问题