【发布时间】: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