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

4

3 回答 3

1

我更新了你的代码

$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>

这是工作小提琴

于 2014-12-03T05:55:41.380 回答
0

它应该工作

$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>
于 2014-12-03T06:00:01.077 回答
0

分叉的工作解决方案:http: //jsfiddle.net/nypmmkrh/

更改您的范围功能:

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

并在视图中传递 id:

<a href="#" ng-click="flipMode(thing.id)">{{thing.id}}</a>
于 2014-12-03T06:00:44.630 回答