0

My html code is

<div>
<div>
<form name="form 1" ng-submit="submitForm1()">
<input type="text" required name="text1">
</form>
</div
<div>
<form name="form 2" ng-submit="submitForm2()">
<input type="text" required name="text2">
</form>
</div>
</div>

and in my controller i am accessing the form using scope.controller looks like

$scope.submitFrom1 = function(){
  console.log(form1);
}

$scope.submitFrom2 = function(){
  console.log(form2);
}

but in result first form will give object and second form is returning undefined I am getting why this is happening.

2 Answers 2

1

You have to use ngModel for the fields inside each form to access data. https://docs.angularjs.org/api/ng/directive/ngModel

<form name="firstForm" ng-submit="submitForm1()">
    <input type="text" required name="text1" ng-model="firstForm.text1>
</form>

<form name="secondForm" ng-submit="submitForm2()">
    <input type="text" required name="text1" ng-model="secondForm.text1>
</form>

In controller

$scope.firstForm.text1 to access the data from first form individually.
$scope.secondForm.text1 to access the data from second form individually.

To get full form object just use.

$scope.firstForm;
$scope.secondForm;
Sign up to request clarification or add additional context in comments.

1 Comment

i am getting the firstForm but secondForm getting is undefined
0

Remove the spaces from the form names.

<!-- remove spaces from form name
<form name="form 1" ng-submit="submitForm1()">
-->
<form name="form1" ng-submit="submitForm1()">
<input type="text" required name="text1" ng-model="a">
</form>
<!-- remove spaces from form name
<form name="form 2" ng-submit="submitForm2()">
-->
<form name="form2" ng-submit="submitForm2()">
<input type="text" required name="text2" ng-model="b">
</form>

It was causing $parse errors.

The DEMO on JSFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.