Make change inside app.config() in angularjs

前端 未结 2 1599
情书的邮戳
情书的邮戳 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: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
    })
    

提交回复
热议问题