Skip to content

Instantly share code, notes, and snippets.

@radius
Created February 19, 2014 21:42
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 radius/9102288 to your computer and use it in GitHub Desktop.
Save radius/9102288 to your computer and use it in GitHub Desktop.
AngularJS module for executing an expression when an element is shown
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