Getting the ng-model name in controller

264 views
Skip to first unread message

Vignesh Kumar

unread,
Oct 13, 2014, 12:54:04 AM10/13/14
to ang...@googlegroups.com
Hi all,

I implemented a custom field in jsp with dynamic ng-model name for that particular field. I need that field's ng-model name in the controller to go ahead to process that field. 

For instance,

<input type = "text" data-ng-model = "clients.2000" />

Here, I need the ng-model name, ie., 2000 which is generated randomly while processing the jsp page... Please help me how to get that model name.. 

Thanks in advance... :-)

dinesh kumar

unread,
Oct 13, 2014, 1:28:40 AM10/13/14
to ang...@googlegroups.com
hi vignesh,

can you tell me the scenario which why do you need the variable name 2000.because accessing the data-ng-model attribute in the controller is not big deal,but that is not recommended,if u can explain the scenario,it will be useful to give you a proper solution.

Regards
Dinesh kumar.L

Vignesh Kumar

unread,
Oct 13, 2014, 1:41:55 AM10/13/14
to ang...@googlegroups.com
Hi Dinesh,

I am using a validation mechanism which makes use of the model name in the controller. The validation for the hard coded fields is not a problem, but I can't get the custom fields in controller for validating. That's the problem, I am facing in this scenario... 

Thanks.... 

Sander Elias

unread,
Oct 13, 2014, 2:50:49 AM10/13/14
to ang...@googlegroups.com

Hi Vignesh,

This can be done fairly simple, but I seriously think you should not be dong this inside a controller. Its a kind of design
that will get you into trouble later on, while extending/maintaining your app.

   var el = angular.element('input');
   var names = [];
   var model = '';
   for (var i=0, l=el.length;i<l; i +=1 ) {
       model = angular.element(el[i]).attr('ngModel'))
       if (model) { names.push(model) }
   }

I din’t test the code, so it might need some fiddling ;)

Regards
Sander

Vignesh Kumar

unread,
Oct 13, 2014, 3:16:00 AM10/13/14
to ang...@googlegroups.com
Hi Sander,

Thanks for making a note.. Got an idea how to work with it...

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/SOKewyBO_4k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--

Thanks & Regards,
Vignesh Kumar. J



Reply all
Reply to author
Forward
0 new messages