gpt4 book ai didi

javascript - Angular ng-if 确定输入数值

转载 作者:行者123 更新时间:2023-12-03 04:56:42 26 4
gpt4 key购买 nike

很简单,只是不太确定如何编码,我有一个输入框,它设置一个值,然后提交该值进行处理,目前我希望输入框中的值默认为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 函数

    $scope.isEmpty = function(number) {         
return (number == undefined || number == null || number == "");
};

因此,在这个阶段,它会执行所有操作,但要么分配值 0,要么分配用户的值。我希望这些值默认为 0,因为它们并非都是强制性的,但表单不允许提交空输入字段。

最佳答案

在scope的初始化中,将staff.staffTotal设置为0。即

$scope.staff.staffTotal = 0;
$scope.isEmpty = function() {
return $scope.staff.staffTotal == 0;
}

关于javascript - Angular ng-if 确定输入数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42405386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com