简体   繁体   English

我如何使用angularjs从$ scope中获取值?

[英]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.

相关问题 我如何使用AngularJS foreach使用JSON中的键获取特定值 - How can I get specific values using a key from json using AngularJS foreach AngularJS:当两者都需要来自父范围的值时,如何避免紧密耦合堆叠指令? - AngularJS : How can I avoid tightly coupling stacked directives when both need values from parent scope? 如何获得 <td> 从angularjs范围动态地到表的值? - How to get <td> values that comes dynamically to table from angularjs scope? 如何区分AngularJS中的$(jQuery)和$ scope? - How can I differentiate between $ (jQuery) and $scope from AngularJS? 如何在AngularJS中从$ scope更改URL? - How can I change URL from $scope in AngularJS? 如何在一个范围内将延迟的对象集包含在AngularJS的另一个范围的摘要中? - How can I get a deferred object set in one scope to be included in the digest of another scope in AngularJS? 如何从angularjs中的$ scope获取普通对象? - How to get plain object from $scope in angularjs? 如何从$ .get jquery更新angularjs $ scope? - How to update angularjs $scope from $.get jquery? 如何在angularJS中访问其他$ scope - How can I access different $scope in angularJS 如何在Angularjs中仅检索可见字段值? 隐藏/删除字段仍在范围内 - How can I retrieve only visible field values in Angularjs? Hidden/Removed fields persist in scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM