在 angularjs 中的 app.config() 内进行更改

2023-12-05

我在 angularJs 中使用 googlePlace api,我想根据需要动态更改地点类型。就像我使用控制器使用 $scope 绑定视图部分中的值一样,但在这种情况下它不起作用也尝试了 $rootScope。

也尝试了很多其他的东西,但它们都不起作用,而且我对 AngularJs 也是新手,所以不太了解。

这是代码:-

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

控制器代码:-

  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;
    });
}

所以基本上我想从视图部分更改“types:[]”数组。

任何帮助,将不胜感激。


您可以将这些默认值集作为常量,以便您可以直接在配置阶段中获取这些值,如角度constant在那里和 Angular 的所有其他组件中都可以访问controller, factory, directive等等,只需注入它的依赖项即可。

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);
});

每当你想改变的值ngGPlacesDefaults配置,您可以通过注入来处理这些值ngGPlacesDefaults依赖性

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

  ngGPlacesDefaults.types = ["some", "different", "values"]; //you could change value like this
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 angularjs 中的 app.config() 内进行更改 的相关文章

随机推荐