I try to create a list of books which may or may not contain comments. So I'd like to have ability to click on comments link (if it contains comments) and to see a list of comments.
I've read that each <li> creates its own scope. So I've tried to create local variable and show/hide comments list depending on click of "comments" link.
For some reason ng-click doesn't work and doesn't change "showComments" variables
I wrote small example to describe problem.
Any suggestions? Thanks.
var app = angular.module('myApp', ['ui.bootstrap']); app.controller('bookCtrl', function($scope) { $scope.books=[ {Name:"Book1",Comments:["first comment book1,second comment book1"]}, {Name:"Book2",Comments:["first comment book2,second comment book2"]}, {Name:"Book3",Comments:[]} ]; }); html, body { width: 100%; height: 100%; } ul{ list-style-type:none; } a:hover { cursor:pointer; } <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-controller="booksCtrl"> <ul> <li ng-repeat="book in books"> <div class="row"> {{book.Name}} </div> <div> <a ng-if="book.Comments.length>0" ng-click="showComments = !showComments ">Comments</a> </div> <div ng-show="showComments"> <ul> <li ng-repeat="comment in book.Comments"> {{comment}} </li> </ul> </div> </li> </ul> </div>