Skip to content Skip to sidebar Skip to footer

How To Reset Incresed Value To It's Initial Value In Angularjs

$scope.count = 23; // this is the initial value {{count}} //Here Value will increase And count value ch

Solution 1:

You can store the initial value in a variable and then reuse it. Here's the fiddle

HTML:

<divng-app="app"ng-controller="MainController">
{{initialValue}}
<buttonng-click="increase()">Increase</button><buttonng-click="reset()">Reset</button></div>

JS:

var app=angular.module('app',[]);
    app.controller('MainController',function($scope){
    var initialValue=20;
    $scope.initialValue=initialValue;
    $scope.reset=function(){
    $scope.initialValue=initialValue;
    };
    $scope.increase=function(){
    $scope.initialValue+=1;
    console.log('Increase value', $scope.initialValue);
    };
    });

Solution 2:

Q1.My question is How do i reset that value to 23 and display ? OR store that value in a variable.

$scope.count = 23;  // this is the initial value<buttonng-click = "count = count + 1"> Increase</button><buttonng-click = "count = 23"> Reset</button>
{{count}}   //Here Value will increase

Q2. Suppose count value increased from 23 to 29 by clicking. And how to get that value 29.

$scope.get =0;
<button ng-click ="get = count">Get</button>
{{get}}

Solution 3:

You should hold a variable in your controller (constant is more appropriate) with which you will reset your count whenever you want.

Controller

$scope.initialValue = 23;
$scope.count = $scope.initialValue;

functionresetCounter() {
    $scope.count = $scope.initialValue;
}

Template

<buttonng-click = "count = count + 1"> Increase </button>

{{count}}

<buttonng-click = "resetCounter()"> Reset </button>

Post a Comment for "How To Reset Incresed Value To It's Initial Value In Angularjs"