Vishal Sankar
Vishal Sankar

Reputation: 11

How can I increment and update button counter in angular JS?

Here it actually increments and once refreshed the values begin from 0.

<div ng-app="myApp" ng-controller="myCtrl">
    <button ng-click=count=count+1>Download</button>
    <p> {{ count }} </p>
</div>

<script>
var  app=angular.module("myApp",[]);
app.controller("myCtrl",function($scope)
        {
    var count=0;
    $scope=count;
        })
</script>
</body>
</html>

I need to increment and update counter.

Thanks in advance.

Upvotes: 0

Views: 274

Answers (2)

Manimaran
Manimaran

Reputation: 11

Your code has lot of typo error. Please use the below code to achieve your output

        <div ng-app="myApp" ng-controller="myCtrl">
           <button ng-click="increment()">Download</button>
           <p> {{ count }} </p>
        </div>

       <script>
       var  app=angular.module("myApp",[]);
       app.controller("myCtrl",function($scope){
          $scope.count = 0;
          $scope.increment = function(){
           $scope.count = count+1;
          };
      });
 </script>
 </body>
 </html>

Upvotes: 1

Tom Edwards
Tom Edwards

Reputation: 124

try putting $scope.count = 0; in your controller

Upvotes: 0

Related Questions