Accessing controller's dom element

21,813 views
Skip to first unread message

kyma

unread,
Sep 9, 2011, 9:41:29 AM9/9/11
to AngularJS
How can I access a controller's dom element, for example if I want to
call a jquery function on a controller's div?

Jon Lester

unread,
Sep 9, 2011, 9:49:52 AM9/9/11
to ang...@googlegroups.com
You can access the controller's object by looking at the $element property of the controller. Once you have the controller you can access the DOM element with $(scope.$element) where scope is your controller object.

Jon

On Fri, Sep 9, 2011 at 9:41 AM, kyma <b...@kymatic.net> wrote:
How can I access a controller's dom element, for example if I want to
call a jquery function on a controller's div?

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.


kyma

unread,
Sep 9, 2011, 9:56:37 AM9/9/11
to AngularJS
Cool, thanks a lot

Igor Minar

unread,
Sep 9, 2011, 12:35:05 PM9/9/11
to ang...@googlegroups.com

$element is going to be killed soon. You should never do any kind of dom manipulation in the controller. Use widget/directives for this purpose.

The reason why controllers should never reference the dom is because in order to avoid spaghetti code and make controllers testable,  they should contain just business logic of your application.

/i

Debjyoti Sarkar

unread,
Jun 18, 2013, 11:46:24 PM6/18/13
to ang...@googlegroups.com
How about setting the focus() on some input element when user submits a Form?
Is that considered to be a DOM manipulation from controller? If yes, how would I achieve this from directive?

I'm following the first example in this link http://docs.angularjs.org/guide/forms

Or should I create a directive for all form handling actions e.g. validating required fields, submission, resetting etc.?

Dimitrios Kanellopoulos

unread,
Aug 17, 2013, 10:49:56 AM8/17/13
to ang...@googlegroups.com
I need to say that when a person reads the documentation examples he gets the impression that the DOM manipulation should be done in the controller as said above. I ve fallen in this pitfall so far due to the examples and the tutorials. 

xin zhang

unread,
Feb 18, 2015, 12:19:20 AM2/18/15
to ang...@googlegroups.com
Hi Igor,

Looks like $element has been killed. Then how do I use $mdToast to show a toast within one specific element? In other words, how can I get the controller's element and assign it to "parent" option of $mdToast? Thanks

Sander Elias

unread,
Feb 18, 2015, 2:52:36 AM2/18/15
to ang...@googlegroups.com
Hi Xin,

There are many ways to resolve that. Most (if not all) of those don't belong in a ngController context(which is the main reason the $element is gone!). You should write an directive to solve your issue.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages