【问题标题】:Angular JS: To get the scope value outside the http callAngular JS:在 http 调用之外获取范围值
【发布时间】:2018-01-30 09:27:13
【问题描述】:

我有多个选择框。对于这些选择框中的每一个,http 调用都有不同的 URL。我需要将响应数据加载到这些框中:

function getFilterDataUrl(category_url) {
		var filterUrl = Config.endpoints.getMilestoneFilter.url+"?filter_name="+category_url;
		 $http({
			  url: filterUrl,
			  method: Config.endpoints.getMilestoneFilter.method
			  
		}).success(function successCallback(response){
			switch(category_url) {
			case 'REGION': $scope.REGION = response.data;
				break;
			case 'SHIPPING_POINT': $scope.SHIPPING_POINT = response.data;
				break;
			}
		});
		
	}
	getFilterDataUrl('REGION');
	getFilterDataUrl('SHIPPING_POINT');
<div class="form-group">
		<label for="pwd">REGION:</label>
		<multiselect class="input-xlarge" multiple="true" ng-model="REGION" options="i in REGION"
		 change="selected()"></multiselect>
	</div>
	
		<div class="form-group">
		<label for="pwd">SHIPPING POINT:</label>
		<multiselect class="input-xlarge" multiple="true" ng-model="SHIPPING_POINT" options="i in SHIPPING_POINT"
		 change="selected()"></multiselect>
	</div>

但是当我尝试使用这个$scope.REGION$scope.SHIPPING_POINT 时,我得到“未定义”。而在成功功能内部,我能够获得价值。请帮助我了解在 getFilterDataUrl() 函数之外获取这些范围值时我做错了什么

【问题讨论】:

  • 使用$rootScope 而不是$scope
  • 不。还是不行
  • 您何时尝试访问这些变量?在触发完整回调之前还是之后?直到成功回调触发后,才会填充这些变量。

标签: javascript angularjs html function closures


【解决方案1】:

在js开头定义$scope.REGION和$scope.SHIPPING_POINT。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2014-03-01
    • 2019-06-07
    • 2016-05-22
    • 1970-01-01
    • 2014-06-07
    • 2015-11-24
    相关资源
    最近更新 更多