0

Not quite sure if this is an anti-pattern maybe someone could shed some light on if there us a better way of doing it.

I have bound a re-size event on the window and also want to trigger the same code on load. IS this the correct way to do this?

angular.element($window).bind("resize", function (event) {

    // code here

}).trigger("resize");

fiddle below.

http://jsfiddle.net/6g49zo7n/

ManyStylez
  • 15
  • 4

1 Answers1

0

The usual approach is to extract the shared code to a single function and call that function from both event types. So it would look like:

function resize(event) {
    // do the resize
}


angular.element($window).bind("resize", resize);

/* now call resize on init the angular way */

To call resize on load the Angular way, take a look at: execute function on page load?.

Community
  • 1
  • 1
Cymen
  • 14,079
  • 4
  • 52
  • 72