0

I want to hide detail of others and show the detail of the current clicked name

http://jsbin.com/tewegahobi/edit?html,js,output

<li ng-click="showDetail = true"  ng-repeat="item in items">{{item.name}}
       <span ng-show="showDetail == true">{{item.detail}}</span>
      </li>

Not sure I'm doing it right, I'm able to click to show the detail, but it doesn't hide others' detail when I clicked on particular name.

1 Answer 1

1

You can make it so that only one name displays at a time by monitoring which item has been clicked with scope. Then you can also use scope to define a function to use with ng-click so that when that user clicks on an item that value changes. Each item has it's ng-show attribute to only show an item if it matches the item that was previously selected by the user.

function TodoCrtl($scope) {
  $scope.items = [{name:"James",detail:"something of James"},{name:"John",detail:"something of John"}]

  // Holds the mame of the item clicked by the user.
  $scope.chosen = '';

  // This function is activated each time the user clicks on an element
  // with 'ng-click' that is associated with this funciton.
  $scope.setChosen = function(itemName) {
    $scope.chosen = itemName;
  }
}
        
<!DOCTYPE html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<meta charset=utf-8 />
<title>ng-click</title>
</head>
<body>
  
<div ng-controller="TodoCrtl">

 <li ng-click="setChosen(item.name)"  ng-repeat="item in items">{{item.name}}
   <span ng-show="item.name == chosen">{{item.detail}}</span>
  </li>

</div>
</body>
</html>

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

2 Comments

I am not sure this is what he wants... He would like to have only one detail shown at the time, I think.
@Mike yes but is it possible not to have a function in the controller level?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.