admin管理员组文章数量:1430644
Typically to validate a form in Angular, I would use something like this on the ng-submit
directive:
<form name="formName" ng-submit="formName.$valid && submitForm()"></form>
This works great when the form has a name that I set myself while building the form. However, in my current situation, I am trying to create multiple forms based on a list of objects. In this case, each form has a name that is determined on the fly.
When the user submits one of these forms, how can I validate it before running the submitForm()
function for that form?
Here's a jsfiddle of the simplified problem: /
My question is, how can I access the name of the form in order to validate it? Here is the code from the fiddle:
var app = angular.module('app', []);
app.controller("AppController", ['$scope', function($scope) {
$scope.forms = [{
id: 1,
value: "val1"
}, {
id: 2,
value: "val2"
}, {
id: 3,
value: "val3"
}];
$scope.submitForm = function() {
alert('submitted');
}
}]);
<script src=".2.1/angular.min.js"></script>
<div id="app" ng-app="app" ng-controller="AppController">
<div class="formWrapper" ng-repeat="form in forms">
<form name="{{ 'form' + form.id }}" ng-submit="submitForm()" novalidate>
<input ng-model="form.value" required />
<button type="submit">Submit</button>
</form>
</div>
</div>
Typically to validate a form in Angular, I would use something like this on the ng-submit
directive:
<form name="formName" ng-submit="formName.$valid && submitForm()"></form>
This works great when the form has a name that I set myself while building the form. However, in my current situation, I am trying to create multiple forms based on a list of objects. In this case, each form has a name that is determined on the fly.
When the user submits one of these forms, how can I validate it before running the submitForm()
function for that form?
Here's a jsfiddle of the simplified problem: http://jsfiddle/flyingL123/ub6wLewc/1/
My question is, how can I access the name of the form in order to validate it? Here is the code from the fiddle:
var app = angular.module('app', []);
app.controller("AppController", ['$scope', function($scope) {
$scope.forms = [{
id: 1,
value: "val1"
}, {
id: 2,
value: "val2"
}, {
id: 3,
value: "val3"
}];
$scope.submitForm = function() {
alert('submitted');
}
}]);
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<div id="app" ng-app="app" ng-controller="AppController">
<div class="formWrapper" ng-repeat="form in forms">
<form name="{{ 'form' + form.id }}" ng-submit="submitForm()" novalidate>
<input ng-model="form.value" required />
<button type="submit">Submit</button>
</form>
</div>
</div>
Share
Improve this question
edited Dec 15, 2017 at 17:46
user2314737
29.5k20 gold badges108 silver badges123 bronze badges
asked Sep 30, 2014 at 19:01
flyingL123flyingL123
8,14613 gold badges74 silver badges138 bronze badges
0
1 Answer
Reset to default 8You can always use this
to access the scope in your templates.
{{this.foo === foo}} <!-- This will always show "true" -->
Therefore, you can simply use this[myDynamicFormName]
to access the form:
<form name="{{'form' + form.id}}" ng-submit="this['form' + form.id].$valid && submitForm()"></form>
本文标签: javascriptAngular validate form with dynamic nameStack Overflow
版权声明:本文标题:javascript - Angular validate form with dynamic name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745545660a2662692.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论