user5503464
user5503464

Reputation:

How can i get values from this $scope using angularjs?

I am getting selecteditems in console like this

[{"model":"Lumia","brand":"Nokia","subModel":["Lumia 735 TS","Lumia 510"],"city":"Bangalore"}] 

i stoed in $scope.details var selecteditems = $location.search().items; $scope.details =[{"model":"Lumia","brand":"Nokia","subModel":["Lumia 735 TS","Lumia 510"],"city":"Bangalore"}]

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

console.log($scope.details.brand);

I should get Nokia

console.log($scope.details.subModel);

I should get "Lumia 735 TS","Lumia 510"

Upvotes: 0

Views: 81

Answers (5)

ashfaq.p
ashfaq.p

Reputation: 5469

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

Upvotes: 0

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22875

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

Upvotes: 0

Mahantesh Kumbar
Mahantesh Kumbar

Reputation: 255

Try this

angular.forEach($scope.details, function(value,key){
   console.log(value.model);
   console.log(value.brand);
   console.log(value.subModel);
})

Upvotes: 0

Jai Prak
Jai Prak

Reputation: 3400

Here you are trying to get value using key of the object. But actually $scope.details here is an array of object. Use this:

$scope.details[0].model;
$scope.details[0].brand;
$scope.details[0].subModel;

You can easily get the values of these.

Upvotes: 0

nikunj2512
nikunj2512

Reputation: 621

You are querying the values wrong.

You have 2 options, either change the data format or query the data properly.

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.

Upvotes: 1

Related Questions