javascript - AngularJs Counter to count up to a specific target number -
i trying create counter using angularjs should count number present in division. here html snippet.
<div class="circle-home"> <span class="circle-home-score " id="counterofreviews" data-count="{{noreviews}}">{{noreviews}}</span> reviews </div> now when trying value inside span {{noreviews}} instead of value.
here angularjs code.
var demoapp = angular.module(['demoapp','ngroute','ui.bootstrap']); demoapp.controller('searchcontroller',function ($scope, $http, $facebook, $interval){ $scope.noreviews=100; $scope.childonload = function() { $scope.uppercount=$("#counterofreviews").text(); $scope.no_reviews=0; console.log($scope.uppercount); var stop; stop = $interval(function() { if ($scope.uppercount >$scope.no_reviews) { $scope.noreviews=$scope.no_reviews; $scope.no_reviews++; console.log('inside if statement'); } else { $scope.stopfight(); } }, 100); }; $scope.stopfight = function() { if (angular.isdefined(stop)) { $interval.cancel(stop); stop = undefined; } }; $scope.childonload(); }; output of console.log($scope.uppercount) {{noreviews}}. unable figure out proper way it. please suggest correction or other better method same perpose.
not sure why use jquery #counterofreviews value. value there because it's added server side script?
as mentioned in comments, code not working because jquery.text() returning string. using parseint(text) work.
please have @ demo below , here @ jsfiddle.
it's more angular , should getting started counter.
var demoapp = angular.module('demoapp', []); //'ngroute','ui.bootstrap']); demoapp.controller('searchcontroller', function ($scope, $http, $interval) { //$facebook, $scope.noreviews = 100; //$scope.childonload = function () { this.uppercount = 10; //$("#counterofreviews").text(); console.log(this.uppercount); var stop; this.startcounter = function () { // needed re-run on change //console.log(stop, this); this.no_reviews = 0; if ( angular.isundefined(stop) ) stop = $interval(checkcount.bind(this), 100); }; this.startcounter(); //}; function checkcount() { if (this.uppercount >= this.no_reviews) { this.noreviews = this.no_reviews; this.no_reviews++; //console.log('inside if statement'); } else { stopfight(); } } function stopfight() { if (angular.isdefined(stop)) { $interval.cancel(stop); stop = undefined; } }; //$scope.childonload(); }); <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="demoapp" class="circle-home" ng-controller="searchcontroller ctrl">review max.: <input ng-model="ctrl.uppercount" ng-change="ctrl.startcounter()"/> <span class="circle-home-score " id="counterofreviews" data-count="{{ctrl.uppercount}}">{{ctrl.noreviews}}</span> reviews</div>
Comments
Post a Comment