让里面angularjs的app.config()的变化 [英] Make change inside app.config() in angularjs

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

问题描述

我正在使用angularJs googlePlace API,我想根据需要动态改变的地方类型。就像我使用的控制器使用$范围绑定鉴于部分价值,但它不是在这种情况下工作也试过$ rootScope。

试了很多其他的东西太多,但他们都没有工作,我也是新angularJs所以不知道了。

下面是code: -

\r
\r

的app.config(功能(ngGPlacesAPIProvider){\r
  ngGPlacesAPIProvider.setDefaults({\r
    半径:1000000\r
types:['electronics_store','bakery','bank','beauty_salon','bicycle_store','book_store','cafe','car_dealer','car_wash','car_repair','clothing_store','dentist','department_store'],\r
nearbySearchKeys:['名','几何','​​参考'],\r
placeDetailsKeys:['名','的formatted_address','FORMATTED_PHONE_NUMBER',\r
        参考,网页,几何,电子邮件]\r
  });\r
});

\r

\r
\r

控制器code: -

\r
\r

的app.config(功能(ngGPlacesAPIProvider,ngGPlacesDefaults){\r
ngGPlacesAPIProvider.setDefaults(ngGPlacesDefaults);\r
});\r
\r
app.controller('scdfindCustomerCtrl',函数($范围,ngGPlacesAPI,$ HTTP,ngGPlacesDefaults){\r
\r
ngGPlacesDefaults.types = [ATM];\r
\r
$ scope.getDetails =功能(REF){\r
$ scope.details = ngGPlacesAPI.placeDetails({参考:REF}),然后(\r
功能(数据){\r
$ scope.det =数据;\r
的console.log(数据);\r
返回的数据;\r
});\r
}\r
\r
$ scope.positions = {[纬度:37.7699298,经度:-122.4469157}];\r
\r
$ scope.addMarker =函数(事件){\r
    VAR LL = event.latLng;\r
\r
    $ scope.positions.push({纬度:ll.lat(),LNG:ll.lng()});\r
  \r
\r
$ scope.data = ngGPlacesAPI.nearbySearch({纬度:ll.lat(),经度:ll.lng()}),然后(\r
    功能(数据){\r
$ scope.person =数据;\r
的console.log(数据);\r
返回的数据;\r
    });\r
}

\r

\r
\r

所以基本上我想改变类型:[]数组从视图部分

任何帮助将是AP preciated。


解决方案

您可以有这些设置默认值作为一个常数,这样你可以直接获取配置阶段内的值,如角是在那边和放大器获得;在角像控制器的所有其他部件工厂指令等只是通过注射它的依赖关系。

  app.constant('ngGPlacesDefaults',{
    半径: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:['名','几何','​​参考'],
    placeDetailsKeys:['名','的formatted_address','FORMATTED_PHONE_NUMBER',
        参考,网页,几何,电子邮件]
  });
})

配置

 的app.config(功能(ngGPlacesAPIProvider,ngGPlacesDefaults){
  ngGPlacesAPIProvider.setDefaults(ngGPlacesDefaults);
});

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

  app.controller('myController的',函数($范围,ngGPlacesDefaults){
  //其他code在这里  ngGPlacesDefaults.types =一些,不同,价值]; //你可以改变值这样的
})

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.

解决方案

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

这篇关于让里面angularjs的app.config()的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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