user4520208
user4520208

Reputation:

How to reset incresed Value to it's initial value in AngularJs

$scope.count = 23;  // this is the initial value


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

{{count}}   //Here Value will increase

And count value changed...

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

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

Upvotes: 0

Views: 998

Answers (3)

Hitesh Kumar
Hitesh Kumar

Reputation: 3698

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);
    };
    });

Upvotes: 1

AranS
AranS

Reputation: 1891

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>

Upvotes: 0

Trung Le Nguyen Nhat
Trung Le Nguyen Nhat

Reputation: 646

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}}

Upvotes: 0

Related Questions