【问题标题】:reset button in angular js角度js中的重置按钮
【发布时间】:2014-04-22 23:39:29
【问题描述】:

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

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

【问题讨论】:

    标签: angularjs radio-button radiobuttonlist


    【解决方案1】:

    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 文档):

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

    【讨论】:

    • 谢谢,单选按钮怎么样?如何重置它们?
    • 这将重置您的示例中的单选按钮jsBin
    • 谢谢汤姆。我不明白为什么人们给我-1:(
    猜你喜欢
    • 2016-05-10
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    相关资源
    最近更新 更多