【问题标题】:AngularJS Disable button on submit - re enable on errorAngularJS禁用按钮提交 - 重新启用错误
【发布时间】:2014-08-01 10:40:34
【问题描述】:

我在动态禁用按钮时遇到问题。

我的 API 休眠 2 秒以模拟超慢连接,我希望看到提交按钮在提交时被禁用,然后重新启用。

HTML - 手动禁用按钮有效...

<button data-ng-disabled="false" data-ng-click="submit()">Schedule Appointment</button>

HTML - 这不起作用

<button data-ng-disabled="{{ isSaving }}" data-ng-click="submit()">Schedule Appointment</button>

控制器:

app.controller('ScheduleController', function ($scope, $http, $window, $timeout)
{
    $scope.formData = {};
    $scope.isSaving = false;

    $scope.submit = function() {
        $scope.isSaving = true;
        console.log('Schedule Appointment Pressed, isSaving', $scope.isSaving);

        $http.post('/api/v1/schedule/', $scope.formData)
        .success(function(data) {
             // Do Stuff
        })
        .error(function(data){
            $scope.isSaving = false;
            console.log('Error, isSaving', $scope.isSaving);
            $scope.errors = data.errors;
        });
    }   
});

一切正常,console.log 输出 isSaving 正确。问题是按钮实际上并没有被禁用,除非我将 ng-disabled="true" 硬编码到 html 中。

【问题讨论】:

  • 尝试用data-ng-disabled="isSaving"替换data-ng-disabled="{{ isSaving }}"
  • @BogdanSavluk - 很简单啊!!!它有效......非常感谢你!我应该如何知道何时需要使用 {{ }} 以及何时不使用它?
  • 这取决于所需的参数类型,在ng-disabled 的情况下,它是在范围内评估的表达式。例如,指令ng-href,需要使用范围内插的模板,在这种情况下,参数可以是任何字符串,可能是{{}}

标签: javascript html ajax angularjs


【解决方案1】:

问题在于您的 HTML。 在 ng 指令中访问范围变量时不需要 {{}}。

<button ng-disabled="isSaving" ng-click="submit()">Schedule Appointment</button>

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 2019-03-19
    • 1970-01-01
    • 2014-11-28
    相关资源
    最近更新 更多