Edit:
As demonstrated in the plunker by Wayne Ellery, the second code sample does actually work. The error was somewhere else in the page.
http://plnkr.co/edit/rtmwOzhiWn0695OGwS9b?p=preview
Original
I'm trying to disable the 'submit' button on a form using AngularJS, however I run into trouble if the form is inside an ng-repeat.
This following code works fine:
<form name="myForm">
<input name="myText" type="text" ng-model="mytext" required />
<button ng-disabled="myForm.$invalid">Save</button>
</form>
however this doesn't, even if there's only one item in the array:
<div ng-repeat="item in data.Items">
<form name="myForm">
<input name="myText" type="text" ng-model="mytext" required />
<button ng-disabled="myForm.$invalid">Save</button>
</form>
<div>
Presumably this is because the name of the form is somehow altered by the repeat. I saw a post which suggest adding {{$index}} to the form name
AngularJs dynamic name for a form inside ng-repeat
But if I do this I'm not sure how to then access the form name within the ng-disabled tag - obviously this won't work:
<div ng-repeat="item in data.items" ng-init="formName = 'myForm' + $index">
<form name="{{formName}}">
<input name="myText" type="text" ng-model="mytext" required />
<button ng-disabled="{{formName}}.$invalid">Save</button>
</form>
</div>
How do I access the correct form to check the validation of the current form?