[英]How can i get values from this $scope using angularjs?
I am getting selecteditems
in console like this 我在这样的控制台中收到
selecteditems
[{"model":"Lumia","brand":"Nokia","subModel":["Lumia 735 TS","Lumia 510"],"city":"Bangalore"}]
i stoed in $scope.details var selecteditems = $location.search().items; 我存储在$ scope.details var selecteditems = $ location.search()。items; $scope.details =[{"model":"Lumia","brand":"Nokia","subModel":["Lumia 735 TS","Lumia 510"],"city":"Bangalore"}]
$ scope.details = [{“ model”:“ Lumia”,“ brand”:“ Nokia”,“ subModel”:[“ Lumia 735 TS”,“ Lumia 510”],“ city”:“班加罗尔”}]
how can i get model and brand and subModel city in above variable 我如何在上述变量中获得模型和品牌以及子模型城市
my expectation : 我的期望:
I have put like this but i am not getting value 我已经这样说了,但我没有获得价值
console.log($scope.details.model);
I should get Lumia 我应该得到Lumia
console.log($scope.details.brand);
I should get Nokia 我应该买诺基亚
console.log($scope.details.subModel);
I should get "Lumia 735 TS","Lumia 510" 我应该得到“ Lumia 735 TS”,“ Lumia 510”
You are querying the values wrong. 您查询的值错误。
You have 2 options, either change the data format or query the data properly. 您有2个选项,可以更改数据格式或正确查询数据。
First approach -> change the data format 第一种方法->更改数据格式
$scope.details ={"model":"Lumia","brand":"Nokia","subModel":["Lumia 735 TS","Lumia 510"],"city":"Bangalore"}
This will allow you to get the values like 这将使您获得像
$scope.details.model
Second approach, if you don't want to change the data format then: 第二种方法,如果您不想更改数据格式,则:
$scope.details =[{"model":"Lumia","brand":"Nokia","subModel":["Lumia 735 TS","Lumia 510"],"city":"Bangalore"}]
You will get the values if you do this 如果这样做,您将获得值
console.log($scope.details[0].model) // value: Lumia.
You data is an array, so you have to pass the index
before you can retrieve the JSON data. 您的数据是一个数组,因此您必须先传递
index
然后才能检索JSON数据。
Since the $scope.details
is an array, you will have to use index to get to the object which contains all the properties you want to access. 由于
$scope.details
是一个数组,因此您将必须使用index来访问包含要访问的所有属性的对象。
$scope.details[0].model
$scope.details[0].brand;
$scope.details[0].subModel;
This means you are accessing the first object in the array and then property model of that object. 这意味着您要访问数组中的第一个对象,然后访问该对象的属性模型。
Here you are trying to get value using key of the object. 在这里,您尝试使用对象的键来获取价值。 But actually
$scope.details
here is an array of object. 但是实际上
$scope.details
是一个对象数组。 Use this: 用这个:
$scope.details[0].model;
$scope.details[0].brand;
$scope.details[0].subModel;
You can easily get the values of these. 您可以轻松获取这些值。
Try this 尝试这个
angular.forEach($scope.details, function(value,key){
console.log(value.model);
console.log(value.brand);
console.log(value.subModel);
})
Your data look like this 您的数据如下所示
$scope.details =[
{
"model":"Lumia",
"brand":"Nokia",
"subModel":["Lumia 735 TS","Lumia 510"],
"city":"Bangalore"
},
{
"model":"Galaxy",
"brand":"Samsung",
"subModel":["Galaxy S1","Galaxy S2", "Galaxy S3"],
"city":"Bangalore"
}
];
So you can do something like 所以你可以做类似的事情
$scope.details.forEach(function(item){
console.log('Model: ', item.model);
console.log('Brand: ', item.brand);
console.log('subModel: ', item.subModel);
console.log('city: ', item.city);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.