angularjs - validate dynamic form before submitting angular -
i'm dynamically creating forms ng-repeat , have validation attributes (simplified version):
<div class="row" ng-repeat="defect in model.defects"> <form name="form_{{defect.id}}" novalidate> <input ng-model="defect.name" required/> <input type="submit" ng-click="savedefect(defect)"/> </form> </div>
basically want this:
$scope.savedefect = function (defect) { if ($scope.<how form name here>.$invalid) { return; } }
since form name has been created dynamically id how access it? other ways of doing same welcome ofcourse :)
you can use bracket notation access :
$scope["form_"+defect.id]
what advise :
var app = angular.module("app", []); app.controller("ctrl", function($scope) { $scope.forms = {}; $scope.list = [{id: 1}, {id: 2}]; $scope.save = function(item) { if ($scope.forms["form_" + item.id].$invalid) { alert("error on form_" + item.id); } } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app="app" ng-controller="ctrl"> <div class="row" ng-repeat="item in list"> <form name="forms.form_{{item.id}}" novalidate> <input ng-model="item.name" required/> <input type="submit" ng-click="save(item)" /> </form> </div> </body>
Comments
Post a Comment