Javascript Angular:如何从$http.get方法外部访问Angular$scope.ng-model_名称?
我正在为$http.get方法使用angularjs代码,但当我尝试访问$scope.metric.label时。它给出的错误类似于Javascript Angular:如何从$http.get方法外部访问Angular$scope.ng-model_名称?,javascript,angularjs,Javascript,Angularjs,我正在为$http.get方法使用angularjs代码,但当我尝试访问$scope.metric.label时。它给出的错误类似于“error:$scope.metric未定义未定义。我想从选择选项创建动态url。但我无法创建动态url 演示 在演示取消注释警报(url)中,您将看到它不工作 //Fetching the metrics and filling the metric selection options $http.get("https:localhost:8080/mete
“error:$scope.metric未定义
未定义。我想从选择选项创建动态url。但我无法创建动态url
演示在演示取消注释警报(url)中,您将看到它不工作
//Fetching the metrics and filling the metric selection options
$http.get("https:localhost:8080/metering/data/")
.success(function(response) {
$scope.metrics = response.nova_meters.concat(response.glance_meters).concat(response.cinder_meters).concat(response.ipmi_meters).concat(response.neutron_meters).concat(response.swift_meters).concat(response.kwapi_meters);
$scope.metric = $scope.metrics[0];
});
// Fetching the list of instances and filling the Group by selection options
$http.get("https:localhost:8080/metering/project/")
.success(function(response) {
//alert(JSON.stringify(response[0].instances));
$scope.groups = response[0].instances;
$scope.group_by = $scope.groups[0];
});
var url = "localhost?" + "meter=" + $scope.metric.label + "&group_by=project" ;
console.log(url);
我想做出如下选择…HTML
<div ng-controller="ceilometerCtrl">
<select ng-model="metric" ng-options="value.label group by value.group for value in metrics" ng-change="update()">
</select>
<select ng-model="group_by" ng-options="value.Id for value in groups" ng-change="update()">
</select>
</div>
您会收到错误
$scope.metric未定义
,因为当您调用
var url=“localhost?”+“meter=“+$scope.metric.label+”&group\u by=project”
您正试图访问一个尚不存在的对象上的标签
.success
回调中的代码是异步执行的,您将只在其回调中收到结果。如果您修改代码以访问$scope.metrics
,您应该能够使用它
var url = "";
//Fetching the metrics and filling the metric selection options
$http.get("localhost/data/")
.success(function(response) {
$scope.metrics = response.nova_meters.concat(response.glance_meters).concat(response.cinder_meters).concat(response.ipmi_meters).concat(response.neutron_meters).concat(response.swift_meters).concat(response.kwapi_meters);
$scope.metric = $scope.metrics[0];
// $scope.metric is available here, you can use it
url = "localhost?" + "meter=" + $scope.metric.label + "&group_by=project" ;
});
//$scope.metric isn't available here (http call hasn't finished yet)
// will print undefined because it was never assigned to
console.log($scope.metric);
由于您的请求是异步的,您必须处理回调来检索一些数据 在angularJs中,$http返回一个承诺,因此您可以组合它们 此外,您还可以使用$q服务和.all()方法。此方法将承诺数组作为参数,并返回将在参数数组中的承诺解析为时解析的承诺 此外,如果设置对象的属性,如$scope.metric,则必须声明该对象,必须定义该对象 控制器
(function(){
function Controller($scope, $http, $q) {
//Declare metric
$scope.metric = {};
var defer = $q.defer();
var url = '';
//Declare promise
var promise1 = $http.get("path_to_url");
var promise2 = $http.get("another_path");
//Declare our function callback to launch when promises are finished
function callback(response){
//response is an array of promises results, in the same order
//reponse[0] is the promise1 result
$scope.metric.name = response[0].data.name;
//response[1] is the promise2 result
$scope.group_by = response[1].data.group;
url = "localhost?" + "meter=" + $scope.metric.name + "&group_by=" + $scope.group_by ;
//Resolve data with the built url
defer.resolve(url);
}
//When all promise are completed, then call callback function
$q.all([promise1, promise2]).then(callback);
//Callback for our resolve result
function print(url){
console.log(url);
}
//Print data when the url is set
defer.promise.then(print);
}
angular
.module('app', [])
.controller('ctrl', Controller);
})();
您第一次在哪里定义
$scope.metric
?听起来您缺少初始化。@Sosdoc在ulr中定义为$scope.metric.label
,请检查它,当它达到$scope.metric时,如果您谈论的是var url=…
,那么它就给出了错误代码>,此时您的代码没有任何$scope.metric的值请参阅演示,只需取消注释警报(url),那么它将不起作用。我明白你的意思,但我将访问外部的选择选项,在我的senario中,我必须通过$http进行多个选择。get..@Neelabhingh在演示中,在定义$scope.metrics
之前,你仍在修改url
,你需要对url做什么?是的,因为我需要更多e不止一个选择选项,在填充这些选择选项后,我将创建url,但正如您所建议的,我将只能创建一个选择选项..因此,为了清楚起见,您希望一个接一个地发送多个http请求?您在末尾创建了一个新的url,并希望再次调用它?感谢您回答了什么是响应[0]
和response[1]
这里,response
是传递到.all()方法数组中的每个承诺的结果数组。结果的顺序与传递参数的顺序相同,因此response[0]
是promise1
是response[0]和response[1]的结果标准?在回调函数中。例如,假设promise1
检索一些度量数组对象,您可以执行$scope.metrics=response[0]
。此外,您可以通过reponse[0].data.myField1
,reponse[0].data.myField2
等方式访问它们。您所说的“标准”是什么意思?