Search code examples
javascriptangularjsng-showng-hide

ng-show Displaying all items


Continue working with angularjs and now having problem with ng-show which on click it show me all hidden data. As I understand, I need to specify ID of clicked item which I want to show, from my example i'm using ng-model which had boolean value and on click it change on true, that's why it's showing all items. Tell me please, how can I show item which I had selected?

<div class="list-group" ng-click="SetItemVisible()" ng-repeat="q in feed">
    <a href="" class="list-group-item">
        <h4 ng-model="showItem" class="list-group-item-heading">{{q.title}}</h4>
        <p ng-show="showItem" value="true" class="list-group-item-text">{{q.body}}</p>
    </a>
</div>

And js:

$scope.SetItemVisible = function () {
    if (!$scope.showItem) {
        $scope.showItem = true;
    } else {
        $scope.showItem = false;
    }
}

$scope.feed = [];

function getRssItems() {
    rssFeedService.getFeed().then(function (results) {
        $scope.feed = results.data;

    }, function (error) {
        //alert(error.data.message);
    });
}

Solution

  • You can do dis by:

    $scope.feed = [{
        'title': "A",
        'body': "testA body"
      },
      {
        'title': "b",
        'body': "testb body"
      }
      ]
        $scope.showItem = {};
      $scope.SetItemVisible = function (index) {
        $scope.showItem[ index] = true;
    
      }
    
     <div class="list-group" ng-click="SetItemVisible($index)" ng-repeat="q in feed track by $index">
    <a href="" class="list-group-item">
        <h4 ng-model="showItem[$index]" class="list-group-item-heading">{{q.title}}</h4>
        <p ng-show="showItem[$index]" value="true" class="list-group-item-text">{{q.body}}</p>
    </a>
    

    For live demo click here: http://plnkr.co/edit/ApI9eb8eQlBdoMUkn8do?p=preview