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:

<div ng-app="app" ng-controller="MainController">
{{initialValue}}
<button ng-click="increase()">Increase</button>
<button ng-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
<button ng-click = "count = count + 1"> Increase</button>
<button ng-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;

function resetCounter() {
    $scope.count = $scope.initialValue;
}

Template

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

{{count}}

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

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