【问题标题】:AngularJS how to disable button after clickAngularJS如何在单击后禁用按钮
【发布时间】:2015-07-28 10:53:42
【问题描述】:

您好,我有一个表单,其中包含一些输入,例如电子邮件、标题、颜色...

我有 2 个按钮“保存”和“撤消更改”。

如果有更改,我需要始终禁用此按钮接受。 例如,我开始更改电子邮件,然后我考虑了。当我输入新的电子邮件按钮时,应该启用,但点击其中一个后,它们应该再次被禁用。

<form name="form" ng-submit="change()" novalidate>
    <input type="text" ng-model="title"/>
    <input type="email" name="email" ng-model="email" placeholder="{{email}}" />
    <input type="color" ng-model="color"/>

    <button type="submit" ng-disabled="!form.$dirty">SAVE</button>
    <a ng-click="cancel()" ng-disabled="!form.$dirty">UNDO CHANGES</a>
</form>

如何在单击其中一个按钮后再次禁用它们?

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    调用$scope.form.$setPristine()取消所有脏标志:

    function TodoCtrl($scope) {
    
        $scope.change = function() {
            $scope.form.$setPristine();
        }
    
        $scope.cancel = function() {
            $scope.form.$setPristine();
        }
    }
    

    示例:http://jsfiddle.net/ej5et0f5/1/

    【讨论】:

      【解决方案2】:

      只需创建一个标志变量,您将在提交后设置为 true,在更改输入后设置为 false,然后执行以下操作:

      <button type="submit" ng-disabled="flagVariable">
      

      【讨论】:

        猜你喜欢
        • 2017-08-30
        • 1970-01-01
        • 2016-12-01
        • 2019-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-25
        相关资源
        最近更新 更多