ng-hide & ng-show in AngularJS

734 views Asked by At

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

There are 3 answers

2
simon On BEST ANSWER

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

0
Lekhnath On

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>
0
user12121234 On

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>