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

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

问题描述

$scope.count = 23;//这是初始值<button ng-click = "count = count + 1" >增加{{count}}//这里的值会增加

并且计数值改变了...

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

第 2 季度.假设计数值通过单击从 23 增加到 29.以及如何获得该值 29.

解决方案

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

HTML:

{{初始值}}<button ng-click="increase()">Increase</button><button ng-click="reset()">重置</button>

JS:

 var app=angular.module('app',[]);app.controller('MainController',function($scope){变量初始值=20;$scope.initialValue=initialValue;$scope.reset=function(){$scope.initialValue=initialValue;};$scope.increase=function(){$scope.initialValue+=1;console.log('增加值', $scope.initialValue);};});

$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.

解决方案

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天全站免登陆