jquery .live() function alternative.

1,169 views
Skip to first unread message

Dania

unread,
Feb 12, 2013, 7:45:57 AM2/12/13
to ang...@googlegroups.com
I am working on converting a jquery project to angularjs. Is there any alternative of .live() function of jquery in angularjs
For example, in jquery if it is like..
 
<script type="text/javascript">
$
(function()
{
        $
(".test").live("mouseover", function()
       
{
                $
(this).css("background-color", "blue");
       
}).live("mouseout", function()
       
{
                $
(this).css("background-color", "white");
       
});
});

What would be its alternative in angularjs? should i use $rootscope over here..

Pawel Kozlowski

unread,
Feb 12, 2013, 7:52:01 AM2/12/13
to ang...@googlegroups.com
Hi!

From what I can see .live is deprecated: http://api.jquery.com/live/
and the "on" should be used instead. AngularJS comes with the light
version of the jQuery (jqLite) that supports the "bind" method that
should do the trick: http://docs.angularjs.org/api/angular.element

Also it is often better to attach event handlers from templates and then the
http://docs.angularjs.org/api/ng.directive:ngMouseenter
http://docs.angularjs.org/api/ng.directive:ngMouseleave
might be handy.

Having said the above AngularJS is _reallllllyyyy_ different as
compared to jQuery in terms of approaching UI construction. So I would
suggest spending some more time getting the feeling of AngularJS
instead of trying to reproduce jQuery code with AngularJS.

The AngularJS-way of coding normally results in dramatically smaller
and more expressive code base.

Hope this helps,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, 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?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Looking for bootstrap-based widget library for AngularJS?
http://angular-ui.github.com/bootstrap/

Ben Nadel

unread,
Feb 12, 2013, 10:03:29 AM2/12/13
to ang...@googlegroups.com
After reading this thread, I got curious to see if I could come up with a declarative "AngularJS way" to use event delegation. I personally really like the way AngularJS uses ngClick and other per-element directives in a $scope-based context. It's really nice, and really powerful.

That said, I thought this would make for an interesting experiment:

http://www.bennadel.com/blog/2448-Using-jQuery-Event-Delegation-In-AngularJS.htm

Essentially, I created a "delegate" directive that pipes a selector into a local scope context.

Currently, it only handles "click" events; but it would be fairly easy to expand.

Cheers,
Ben Nadel
Reply all
Reply to author
Forward
0 new messages