非常简单,只是不太确定如何编码,我有一个输入框设置一个值,然后提交值进行处理,目前我希望输入框中的值默认为0,如果它当前未分配值,即10。
<div class="row">
<div class="col-sm-8">
<label for="totalStaff" style="padding-bottom: 10px; width: 150px;">Number of Staff</label>
<button class="form-button" ng-click="staff.staffTotal = staff.staffTotal - 1" ng-disabled="staff.staffTotal === 0">-</button>
<input class="form-control max-width" type="number" id="totalStaff" name="totalStaff" min="0"
ng-model="staff.staffTotal"
ng-blur="myform.totalStaff.$touched=true"
ng-required="true"
ng-class="{'input-error': myform.totalStaff.$touched && myform.totalStaff.$error.required}"
ng-if="isEmpty"
/>
<button class="form-button" ng-click="staff.staffTotal = staff.staffTotal + 1">+</button>
</div>
</div>
controller isEmpty function
$scope.isEmpty = function(number) {
return (number == undefined || number == null || number == "");
};
所以在这个阶段它会做所有事情,但要么分配值0,要么分配给用户的值。我希望值默认为0,因为它们并非都是必需的,但表单不允许使用空输入字段提交。
答案 0 :(得分:1)
在范围的初始化中,将staff.staffTotal设置为0。 即。
$scope.staff.staffTotal = 0;
$scope.isEmpty = function() {
return $scope.staff.staffTotal == 0;
}
答案 1 :(得分:0)
我认为Angular的方式是将控制器中的staff.staffTotal初始化为0。
否则我只需要添加值=&#34; 0&#34;
答案 2 :(得分:0)
试试这个
在html中将ng-if
更改为ng-if="isEmpty(staff.staffTotal)"
$scope.isEmpty = function(number) {
if(number == 10){// you can change it however you want
$scope.staff.staffTotal == 0;
return true;
}else{
$scope.staff.staffTotal == number;
return true;
}
};