我在这样的 View 中使用ng-disabled我有真实条件
md-button.md-primary.md-raised flex="20" ng-disabled="form.$invalid || form.$pristine || day == 0 || leaveapplication.reason == null" ng-click="save(starts_on, ends_on, leave_type_id, period1, period2)" Apply
所以这次我要在ng禁用状态下使用function:
md-button.md-primary.md-raised ng-disabled="buttonChecker()" ng-click="save(starts_on, ends_on, leave_type_id)" Apply
现在如何将我的条件放入
form.$valid
和form.$pristine
这样的函数中,另一个条件像day == 0
一样容易,但是其他条件对我来说很难。 最佳答案
只需使用$scope
访问它,其中表单名称已绑定(bind)到$scope.nameOfForm
。我不知道您的其他变量(例如day
或leaveapplication.reason
)在哪里定义,因此我只是空白地插入了它们。您必须编辑此部分才能使其正常工作,但是我认为这将向您展示如何实现您的目标。
View
<div ng-controller="MyCtrl">
<form name="myForm">
<input type="text" name="test" ng-model="test">
<button ng-disabled="buttonChecker();">
Some Button
</button>
</form>
</div>
AngularJS应用
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', function ($scope) {
$scope.buttonChecker = function () {
return $scope.myForm.$invalid
|| $scope.myForm.$pristine
|| $scope.day === 0
|| $scope.leaveapplication.reason === null;
};
});
> Demo fiddle