我有一个角度js的问题。
我的问题是: 我有一个带输入的多步骤表单,我也有一个选择字段,通过选择的值我显示按钮,我可以添加另一个输入字段。我使用 ng-disabled =" userForm。$ invalid" ,但问题是他在其他形式的步骤中捕获所有其他输入。如何验证。
提前致谢并抱歉我的英文!
app.js
var validationApp = angular.module('validationApp', []).config(function ($interpolateProvider) {
$interpolateProvider.startSymbol('<%').endSymbol('%>');
});
validationApp.controller('mainController', function ($scope, $compile) {
$scope.log = function () {
console.log($scope.userForm);
};
/*
* If is selected partnership will show button for adding partners.
*/
$(".category").click(function () {
var value = $(".category option:selected").val();
var token = $('.csrf').val();
var solo = 1;
var partnership = 2;
if (value == solo || value == '') {
$('.partner-button').hide();
$('.inputs').remove();
} else if (value == partnership) {
$('.partner-button').show();
//$(".input_fields_wrap").append(fields);
//$compile(fields)($scope);
}
});
/*
* Adding fields for partner.
*/
var count = 0;
$('.add_field_button').click(function (e) {
e.preventDefault();
var input = $("<div class=\"inputs\">" +
"<input type=\"text\" ng-model=\"partnerName" + count + "\" name=\"partners[" + count + "][name]\" placeholder=\"Наименование на партньор\" required/>" +
"<p ng-show=\"userForm['partners[" + count + "][name]'].$error.required\" class=\"errors\">Наименованието на партньора е задалжително</p>" +
"<input type=\"text\" ng-model=\"partnerUrn" + count + "\" name=\"partners[" + count + "][urn]\" placeholder=\"БУЛСТАТ (ЕИК) на партньора\" required>" +
"<p ng-show=\"userForm['partners[" + count + "][urn]'].$error.required\" class=\"errors\">Полето БУЛСТАТ е задалжително</p>" +
"<br><a href=\"#\" class=\"font remove_field\">Премахни</a>" +
"</div>");
count++;
$(".input_fields_wrap").append(input);
$compile(input)($scope);
});
$('.input_fields_wrap').on("click", ".remove_field", function (e) { //user click on remove text
e.preventDefault();
$(this).parent('div').remove();
count--;
});
});
HTML表单第一步
<fieldset>
<h2 class="fs-title">ПРОВЕРКА НА КАНДИДАТ</h2>
<div class="application-type">
<h2 class="fs-title">Тип на кандидатстване: </h2>
<select
name="category"
class="category"
ng-model="category"
required
>
<option value="">Изберете тип на кандидатстване</option>
<option value="1">Самостоятелно</option>
<option value="2">В партньорство</option>
</select>
<p ng-show="userForm.category.$error.required" class="errors">Моля изберете тип на кандидатстване</p>
</div>
<p>
<input type="text"
name="urn"
placeholder="БУЛСТАТ(ЕИК)"
ng-model="urn"
ng-minlength="9"
ng-maxlength="13"
required
ng-class="{ 'has-error' : userForm.urn.$invalid && !userForm.urn.$pristine }"/>
<p ng-show="userForm.urn.$error.required" class="errors">Полето БУЛСТАТ е задалжително</p>
<p ng-show="userForm.urn.$error.minlength" class="errors">Невалиден БУЛСТАТ</p>
<p ng-show="userForm.urn.$error.maxlength" class="errors">Невалиден БУЛСТАТ</p>
</p>
<div class="input_fields_wrap"></div>
<button class="partner-button action-button add_field_button">Добави партньор</button>
<input type="button" name="next" class="next action-button" ng-disabled="userForm.$invalid" value="Напред"/>
<div class="g-recaptcha" data-sitekey="6LfUWRkTAAAAABPcgj4QN-LZzrmv4ZvGDqNGInxN"></div>
答案 0 :(得分:1)
您可以使用ngForm &#34;别名&#34; (如果您不考虑ng-submit doesn't work with ng-form){{ 3}}以便将步骤组合在一起。
这样,每个带有已定义名称的 ngForm 就像 form 一样,将获得自己的表单控制器,从而允许您使用ng-disabled="firstStep.$invalid"
,{{1等等。