当$ setValidity设置为false时,模型得到'undefined'

时间:2015-07-03 04:18:23

标签: javascript angularjs angularjs-scope

我有一个带有一个输入字段的表单,并且当值使用$ setValidity更改时我验证了该输入。但问题是当$ setValidity设置为false(无效)时,模型不再更新,它保持值' undefined'。

这是我的HTML:

<form name="frmEnrollPaymentPlan" novalidate  autocomplete="off">
  <div class="form-group" ng-class="{'has-error':(frmEnrollPaymentPlan.dwnAmt.$invalid && !frmEnrollPaymentPlan.dwnAmt.$pristine)||frmEnrollPaymentPlan.dwnAmt.$error.validateDownPaymentAmount}">
    <label>Downpayment</label>
    <input type="text" class="form-control" name="dwnAmt" ng-model="EnrollPaymentPlanViewModel.DownPaymentAmount" required ng-change="downPaymentChanged()" ng-model-options="{allowInvalid: true}">
    <span ng-show="frmEnrollPaymentPlan.dwnAmt.$error.validateDownPaymentAmount" ng-class="{'error':frmEnrollPaymentPlan.dwnAmt.$error.validateDownPaymentAmount}">Cannot be less than down payment amount</span>
  </div>
</form>

这是我的javascript代码:

$scope.downPaymentChanged = function () {
if (!isNaN($scope.EnrollPaymentPlanViewModel.DownPaymentAmount)) {
    var downPaymentAmount = parseFloat($scope.EnrollPaymentPlanViewModel.DownPaymentAmount);
    var minDownPaymentAmount = parseFloat($scope.EnrollPaymentPlanViewModel.MinDownPaymentAmount);

    if (downPaymentAmount < minDownPaymentAmount) {
        $scope.frmEnrollPaymentPlan.dwnAmt.$setValidity("validateDownPaymentAmount", false);    
    }
    else {
        $scope.frmEnrollPaymentPlan.dwnAmt.$setValidity("validateDownPaymentAmount", true);   
    }
}

}

0 个答案:

没有答案