I'm currently using the following.
$scope.$$childHead.customerForm[firstName]
, so that:
<form name="customerForm">
<input type="text" name="firstName"
ng-model="data.customer.firstName"
tabindex="1"
ng-disabled="!data.editable"
validationcustomer />
</form>
But this only works in Chrome. Now I tried the following:
$scope.editCustomerForm[firstName]
, so that:
<form name="customerForm" ng-model="editCustomerForm">
<input type="text" name="firstName"
ng-model="data.customer.firstName" tabindex="1"
ng-disabled="!data.editable"
validationcustomer />
</form>
Which doesn't work. Note my form is inside a Foundation Tab. How can I access firstName
?
EDIT: It looks like the form
isn't added to the scope
when it's inside a Foundation Tab.
Anyone has got a solution for this?
Definitely you can't access form in scope bec. it is not created. The DOM from html template is loaded little bit slowly like controller constructor. the solution is to watch until DOM loaded and all the scope defined!
in controller:
Though alluded to in other comments I thought I'd spell it out a bit for those using the "Controller As" syntax:
Then you can access the FormController in your code like:
If you want to pass the form to the controller for validation purposes you can simply pass it as an argument to the method handling the submission. Use the form name, so for the original question it would be something like:
Yes, you can access a form in the controller (as stated in the docs).
Except when your form is not defined in the controller scope and is defined in a child scope instead.
Basically, some angular directives, such as
ng-if
,ng-repeat
orng-include
, will create an isolated child scope. So will any custom directives with ascope: {}
property defined. Probably, your foundation components are also in your way.I had the same problem when introducing a simple
ng-if
around the<form>
tag.See these for more info:
https://groups.google.com/forum/#!topic/angular/B2uB8-9_Xbk
AngularJS - losing scope when using ng-include
Note: I suggest you re-write your question. The answer to your question is yes but your problem is slightly different:
To which the answer would simply be: no.
Yes, You can access your from from controller via using this.formname.
add
ng-model="$ctrl.formName"
attribute to your form, and then in the controller you can access the form as an object inside your controller bythis.formName