The fastest is to use events instead of watches but try to stay away from
broadcast on rootscope. It's costly as each and every scope in your app
will be informed about the event. A much better (as in faster) pattern is
to emit on rootscope and to listen on rootscope in your inner scopes. It
does mean you're polluting the rootscope but it's a small price to pay for
a big gain.
Another advice is to debounce (or throttle) your resizeHandler. The resize
event is triggered many times a second and most of the time you don't need
the size updated so often. If you're using underscore, it's a matter of
doing this:
angular.element($window).bind('resize', _.debounce(resizeHandler, 500));
Cheers,
Dan
On Monday, March 31, 2014 7:36:02 PM UTC+3, Olivier Clément wrote:
>
> Hi,
>
> I have a service that already binds to $window's Resize event, and I
> expose the Window's Height through the service
>
> I have multiple directives that need to do stuff when the window is
> resized, and I currently $watch(servc.winHeight) to do what I need
>
> I was wondering if this approach was actually better in term of
> performance, versus binding the resize event of the windows multiple times.
>
> basically it boils down to this:
>
> 1- (simplified code)
> angular.service('winStats', function($window) {
> var resizeHandler = function() {
> this.size = { height: $win.height()}
> }
> angular.element($window).bind('resize', resizeHandler);
> });
>
>
> Then from controllers or directives inject 'winStats', and watching like
> so (assume I need to do this in 3-5 different places/directives that serves
> different purposes):
>
> $scope.$on(function() { return winStats.height;}, function(height) { [...]
> });
>
>
> ----------------
>
> 2- from any directives I need the window's height, simply bind a new
> handler in them:
> angular.element($window).bind('resize', resizeHandler);
>
> ---------------
>
> 3- Or maybe leverage $rootScope.$broadcast from the Service (in option1)
> and doing $scope.$on from the directives?
>
>
>
>
>
>
>
> Thanks for your inputs
>
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.