角度js中的重置按钮

时间:2014-03-17 16:29:02

标签: angularjs radio-button radiobuttonlist

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

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

1 个答案:

答案 0 :(得分:11)

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

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

然后将模型设置为空对象

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

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

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

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

    $scope.reset();
}