在AngularJS中,如何在按钮中设置ng-disabled
属性,以便在用户输入小数输入时不会启用它?
目前,当我输入“0.1”但该按钮已启用时,该框以红色突出显示。
<input type="number" ng-model="person.age" id="age" name="age" placeholder="Age"
class="form-control" min="0" ng-change="compute()" ng-required="!person.age">
<button id="ok-btn" class="btn btn-primary" ng-click="okEdit(person)"
ng-disabled="(!person.age && person.age !== 0)">OK</button>
答案 0 :(得分:1)
检查person.age
值是否等于解析值,如:
angular.module('app', []).controller('ctrl', function($scope) {
$scope.person = {};
$scope.isDisabled = function(person) {
return parseInt(person.age) !== person.age
}
})
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<input type="number" ng-model="person.age" name="age" placeholder="Age" class="form-control" min="0" ng-required="!person.age">
<button class="btn btn-primary" ng-click="okEdit(person)" ng-disabled="isDisabled(person)">OK</button>
</div>