Click here to Skip to main content
15,921,179 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im using ionic to display a small description of the selected item from the list. The problem here is when the item1 from list is selected it displays its description but at same time when the item2 from the list is selected it displays the description of previous one and second one also. I tried many ways but couldn't over come the mistake.


JavaScript
  $scope.items=[];
  $scope.carList = [{ value: "Bmw", description: "Its bmw" },
  { value: "Mercedes", description: "Its Mercedes" },
  { value: "Honda", description: "Its Honda" }];
  $scope.select_item = function (key) {
  $scope.items.push(key);

}



sample work

What I have tried:

I have created a list with description of each item in it, when the item is selected it displays the description of the item.But can't delete and display the description properly .
Posted
Updated 3-Oct-16 3:52am

1 solution

So i think your problem lies in this bit of code.

JavaScript
$scope.items.push(key);


You continually use .push to add items to your $scope.items array. If your $scope.items array is only meant to show one item you need to re-initialize it inside your select_item function to clear it out.

So you should be able to do

JavaScript
$scope.select_item = function(key) {
   $scope.items = []; // clears out previous selection to make empty array
   $scope.items.push(key);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900