在 angularjs 中的 app.config() 中进行更改 [英] Make change inside app.config() in angularjs

查看:21
本文介绍了在 angularjs 中的 app.config() 中进行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

这是代码:-

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:[]"数组.

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

任何帮助将不胜感激.

推荐答案

你可以将这些默认值设置为一个常量,这样你就可以在配置阶段直接获取这些值,作为 angular constant 可以在那里访问 &在 angular 的所有其他组件中,例如 controllerfactorydirective 等,只需注入它的依赖项即可.

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.

恒定

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

每当您想更改 ngGPlacesDefaults 配置的值时,您都可以通过注入 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
})

这篇关于在 angularjs 中的 app.config() 中进行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆