javascript - 如何在 AngularJs 中将增加的值重置为其初始值

标签 javascript angularjs angularjs-ng-click

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


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

{{count}}   //Here Value will increase

计数值发生变化...

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

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

最佳答案

您可以将初始值存储在变量中,然后重复使用它。这是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);
    };
    });

关于javascript - 如何在 AngularJs 中将增加的值重置为其初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37831737/

相关文章:

javascript - 使用动态变量作为对象文字,jQuery 动画函数

php - Wordpress 中的前端媒体上传

angularjs - 在表中嵌套指令

javascript - AngularJS ng-repeat ng-click

javascript - AngularJS 将字符串作为函数传递给 ng-click

javascript - 使用 ng-click 在 angularJs 中添加和删除类

javascript - 如何在可拖动空间上拖动文本?

javascript - 使用 mocha 和 assert 对 NaN 进行正确的单元测试

javascript - Cordova Instagram 插件 : using with JPG images?

angularjs - setColumnDefs 不适用于某些农业网格