Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do ng-click, ng-mouseover etc create watchers and slow down the page? Is it better than jQuery event binding?

I was wondering if directives like ng-click, ng-mouseover etc when used in large numbers throughout the application, would result in performance issues similar to that of ng-repeat?

I am developing an application with AngularJS. I am already loaded with problems because of ng-repeat and the number of watchers it creates. The performance has been affected, and I'm working on it.

like image 646
Kumar Avatar asked Apr 02 '14 15:04

Kumar


People also ask

Can we use NG-click and Onclick together?

For a single btn, it's ok to use ng-click or onclick in the ng-app . There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click is recommended.

What is Ng-Click directive in AngularJS give example for Ng attributes in detail with an example?

The ng-click Directive in AngluarJS is used to apply custom behavior when an element is clicked. It can be used to show/hide some element or it can pop up an alert when the button is clicked. Parameter Value: expression: It specifies when the particular element is clicked then the specific expression will be evaluated.

What is mouseover in angular?

Definition and Usage The ng-mouseover directive tells AngularJS what to do when a mouse cursor moves over the specific HTML element. The ng-mouseover directive from AngularJS will not override the element's original onmouseover event, both will be executed.


1 Answers

There are no extra watches created as part of angular's event directives.

The event directives are fairly straight forward and use jquery (or jqLite if jquery isn't included) to add the event listener to the element with the directive (i.e. ng-click). Here is the code that sets up the event directives in angular. As you can see they do not create a watch.

var ngEventDirectives = {};
forEach(
  'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup keypress submit focus blur copy cut paste'.split(' '),
  function(name) {
    var directiveName = directiveNormalize('ng-' + name);
    ngEventDirectives[directiveName] = ['$parse', function($parse) {
      return {
        compile: function($element, attr) {
          var fn = $parse(attr[directiveName]);
          return function(scope, element, attr) {
            element.on(lowercase(name), function(event) {
              scope.$apply(function() {
                fn(scope, {$event:event});
              });
            });
          };
        }
      };
    }];
  }
);

Keep in mind, a $scope.apply() kicks off a digest loop where the angular async queue is processed and watch list is iterated over.

like image 57
Patrick Avatar answered Sep 19 '22 09:09

Patrick