javascript - AngularJS: Iterate over array by clicking button -


sorry dump, question, new angularjs , javascript. iterate on collection clicking button.

<body ng-init="customers = [ {'name':'john', 'city':'doe'}, {'name':'anna', 'city':'smith'}, {'name':'peter', 'city':'jones'} ]">   <div class="container" ng-app="myapp" ng-controller="myctrl" >     <div class="span12">         <h1>{{name}}</h1>         <br/>         <p>{{city}}</p>           <button type="button" name="button" value="next" ng-click="makeiterator($scope.customers)"></button>     </div> </div> 

so after clicking next button see next iteration of customers displayed. how make it?

you can store index , increment when clicking on button

<body ng-app="myapp"> <div class="container" ng-controller="myctrl" >    <div class="span12">       <h1>{{customers[currentcustomeridx].name}}</h1>       <br/>       <p>{{customers[currentcustomeridx].city}}</p>     <button type="button" name="button" value="next"            ng-click="index = (index + 1) % customers.length"></button> </div> 

<script>  angular.module('myapp', [])  .controller('myctrl', function ($scope) {     $scope.customers = [      {'name':'john', 'city':'doe'},      {'name':'anna', 'city':'smith'},      {'name':'peter', 'city':'jones'}];     $scope.currentcustomeridx = 0;   }); </script>  

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -