Created
August 4, 2017 20:45
-
-
Save b4dnewz/a07ef5b6e7630ed67203ec6d30ead7c3 to your computer and use it in GitHub Desktop.
Angular directive to fire callback on element load.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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