Created
February 19, 2014 21:42
-
-
Save radius/9102288 to your computer and use it in GitHub Desktop.
AngularJS module for executing an expression when an element is shown
This file contains 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
angular.module('OnShow', []). | |
directive('onShow', function () { | |
return { | |
controller: function($scope) { | |
$scope.fireOnce = false; | |
this.setFireOnce = function(bool) { | |
$scope.fireOnce = bool; | |
} | |
}, | |
link: function(scope, elm, attr) { | |
var watcher = scope.$watch(attr.ngShow, function ngShowWatchAction(visible){ | |
console.log(scope); | |
if(visible) { | |
scope.$eval(attr.onShow); | |
if(scope.fireOnce) { | |
console.log('watcher killed'); | |
watcher(); // kill the watcher | |
} | |
} | |
}); | |
} | |
} | |
}). | |
directive('onShowFireOnce', function() { | |
return { | |
require: 'onShow', | |
link: function(scope, element, attrs, onShowCtrl) { | |
onShowCtrl.setFireOnce(true); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment