如何在AngularJs中将增量值重置为其初始值 [英] How to reset incresed Value to it's initial value in AngularJs

查看:123
本文介绍了如何在AngularJs中将增量值重置为其初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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

{{count}}   //Here Value will increase

计数值已更改...

Q1.我的问题是如何将该值重置为23并显示?或将该值存储在变量中.

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

Q2.假设通过单击将计数值从23增加到29.以及如何获得该值29.

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

推荐答案

您可以将初始值存储在变量中,然后再使用它.这是小提琴

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

这篇关于如何在AngularJs中将增量值重置为其初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆