Skip to content

Instantly share code, notes, and snippets.

@b4dnewz
Created August 4, 2017 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b4dnewz/a07ef5b6e7630ed67203ec6d30ead7c3 to your computer and use it in GitHub Desktop.
Save b4dnewz/a07ef5b6e7630ed67203ec6d30ead7c3 to your computer and use it in GitHub Desktop.
Angular directive to fire callback on element load.
'use strict'
###*
# @ngdoc directive
# @name app.directive:ngLoad
# @description Angular directive to fire callback on element load.
# # ngLoad
###
angular.module 'app'
.directive 'ngLoad', ($parse) ->
restrict: 'A'
link: (scope, element, attrs) ->
# get user function
fn = $parse(attrs.ngLoad)
# the callback function
callback = (e) ->
scope.$apply( () -> fn(scope, { $event: e }) )
# bind callback to load event
element.on( 'load', callback )
# remove callback on destroy
scope.$on('$destroy', () -> element.off('load', callback) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment