本文介绍了Angular JS中的重置按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个清除"按钮,一旦用户点击它,容器中的所有数据,所有绑定和单选按钮都应重置(就像最初一样).当前,只有视图变为空,但是容器仍然具有旧值.我该如何解决?

I have a "clear" button, once user hit it all the data in container,all binding and the radio buttons should be reset (like initially). Currently only the view becomes empty but the container has still the old value. How can I fix it?

<div class="field">
         <textarea name="price" ng-model="list.price"></textarea>
</div>

 <input type="radio" ng-model="list.phone" value="1" />cell
 <input type="radio" ng-model="list.phone" value="2" />home

<button class="btn btn-primary btn-large center" type="reset"  ng-click="">
                        Clear
</button>

推荐答案

ng-click 设置为某些功能,例如 reset()

Set ng-click to some function, say reset()

<button class="btn btn-primary btn-large center"
        type="reset"
        ng-click="reset()">Clear
</button>

然后将模型设置为空对象

and then set the model to an empty object

$scope.reset = function() {
    $scope.list = {};
}

或者,如果预先填充了$ scope.list,则可以执行以下操作(取自angular文档):

Or, if $scope.list is prepopulated, you could do something like this (taken from angular docs):

function Controller($scope) {
    $scope.master = {};

    $scope.reset = function() {
      $scope.list = angular.copy($scope.master);
    };

    $scope.reset();
}

这篇关于Angular JS中的重置按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:51