当通过<button> ng-click更新值时,ng-valid不会触发隐藏的输入

时间:2016-01-05 11:43:37

标签: javascript angularjs validation

我是AngularJS的新手。我有一个简单的表单,如果所有必填字段都是ng-valid,则提交按钮变为启用状态。如果我输入LOAN AMOUNT框,则将ng-valid类添加到此输入中。

但是,在第二个字段中,我使用按钮来使用ng-click更新隐藏字段的值。这样可以在点击时更新值,但是类仍然是ng-invalid并且提交按钮被禁用。我猜它是以某种方式与Angular不知道这已经更新,但我无法找到解决方案。我将不胜感激任何帮助。

<body ng-app="validationApp" ng-controller="mainController">
<form name="userForm" id="appform" ng-submit="submitForm(userForm.$valid)" action="results.php" method="post" novalidate validate-non-visible-controls> <!-- novalidate prevents HTML5 validation since we will be validating ourselves -->
<div class="container" style="max-width:1000px;">
    <div class="row">
        <div class="col-md-4">
            <!-- LOAN AMOUNT -->
                <div class="form-group" ng-class="{ 'has-error' : userForm.amount.$invalid && !userForm.amount.$pristine }">
                    <label>How much do you need? <span class="red">*</span></label>
                    <input type="number"  name="amount" id="amount" class="form-control searchfield" ng-model="user.amount" ng-maxlength="6" required>
                    <p ng-show="userForm.amount.$invalid && !userForm.amount.$pristine" class="help-block">Loan amount is required.</p>
                    <p ng-show="userForm.amount.$error.maxlength" class="help-block">Loan amount too high.</p>
                </div>
        </div>
        <div class="col-md-8">
            <!-- BUSINESS TYPE -->
            <div class="form-group" ng-class="{ 'has-error' : userForm.type.$invalid && !userForm.type.$pristine }">
                <label>What type of business do you own? <span class="red">*</span></label>
                <input type="hidden" name="type" ng-model="data.type" value="{{selected}}" required/>
                <button type="button" class="ltd opt" ng-click="selected = 1;userForm.type.$valid" ng-class="{active: selected == 1}">Limited Company</button>
                <button type="button" class="st opt" ng-click="selected = 2" ng-class="{active: selected == 2}">Sole Trader</button>
                <button type="button"class="ps opt" ng-click="selected = 3" ng-class="{active: selected == 3}">Partnership</button>
                <button type="button"class="llp opt" ng-click="selected = 4" ng-class="{active: selected == 4}" >LLP</button>
                <p ng-show="userForm.type.$invalid && !userForm.type.$pristine" class="help-block">Business type is required.</p>
            </div>
        </div>
    </div><button type="submit" class="bluebtn biggerBtn" ng-disabled="userForm.$invalid">FINANCE MY BUSINESS <i class="fa fa-caret-right"></i></button>
</body>

1 个答案:

答案 0 :(得分:0)

您必须将ng-model更改为:

<input type="hidden" name="type" ng-model="selected" value="{{selected}}" required/>