0

Using AngularJS I want to show and hide the data related with particular id in the toggle way.

My JSON Data format is like:

  $scope.things = [{
        id: 1,
        data: 'One',
        shown: true
    }, {
        id: 2,
        data: 'Two',
        shown: false
    }, {
        id: 3,
        data: 'Three',
        shown: true
    },  ];

What I want is when click on id-1 It will show text One and Hide the others, when click on id-2 will show text Two and hide others and so on.

Here is the fiddle what I tried : jsfiddle : Demo Link

3 Answers 3

1

i updated your code

$scope.flipMode = function (id) {
    $scope.things.forEach(function (thing) {
                 if(id == thing.id){
                     thing.shown = true;
                 }
                 else{
                     thing.shown = false;
                 }
    })
};


<a href="#" ng-click="flipMode(thing.id)">{{thing.id}}</a>

here is the working fiddle

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

2 Comments

Thanks. Is it any way to display the related data just below that id. ?
<div ng-repeat="thing in things"> <a href="#" ng-click="flipMode(thing.id)">{{thing.id}}</a> <div>{{thing.data}}</div> </div>
0

It should work

$scope.flipMode = function (id) {
        $scope.things.forEach(function (thing) {
                     if(thing.id === id) {
                         thing.shown = true;
                         return;
                     }

                     thing.shown = false;
        })
    };

<div ng-repeat="thing in things">

   <a href="#" ng-click="flipMode(thing.id)">{{thing.id}}</a>
</div>

Comments

0

Forked working solution: http://jsfiddle.net/nypmmkrh/

Change your scope function:

$scope.flipMode = function (id) {
  $scope.things.forEach(function(thing) {
    if(thing.id == id) {
      thing.shown = true;
    } else {
      thing.shown = false;
    }            
  });   
};

And pass the id in the view:

<a href="#" ng-click="flipMode(thing.id)">{{thing.id}}</a>

Comments

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.