0

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"

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i am getting undefined for 2nd condtion
You mean second solution? i double checked it and it is working. Please check it again
0

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.

8 Comments

i am getting undefined
when i pass directly is working $scope.details =[{"model":"Lumia","brand":"Nokia","subModel":["Lumia 735 TS","Lumia 510"],"city":"Bangalore"}] if i pass $scope.details =$location.search().items; console.log($scope.details); console i am getting value [{"model":"Lumia","brand":"Nokia","subModel":["Lumia 735 TS","Lumia 510"],"city":"Bangalore"}]
$scope.details =JSON.parse($location.search().items);
for this $scope.selectedlist = ["Lumia 510,Lumia 735 TS"]; how can i get like this $scope.selectedlist = ["Lumia 510","Lumia 735 TS"];
Please mark it as answer if it solves your problem. for your next question, you can do $scope.selectedlist[0].split(',').
|
0

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.

1 Comment

$scope.details= $location.search().items; console.log($scope.details);console i am getting like this [{"model":"Lumia","brand":"Nokia","subModel":["Lumia 735 TS","Lumia 510"],"city":"Bangalore"}]
0

Try this

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

Comments

0

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

7 Comments

if i put $scope.details=$location.search().items; $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); }); getting error TypeError: $scope.details.forEach is not a function
it means that $location.search().items is not returning list and it your given sample data, it was list
console i am getting [{"model":"Lumia","brand":"Nokia","subModel":["Lumia 735 TS","Lumia 510"],"city":"Bangalore"}]
is is a string or list object
string with list object
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.