Skip to content

Instantly share code, notes, and snippets.

@jrencz
Created January 7, 2016 11:49
Show Gist options
  • Save jrencz/8e6cb494ebf4d8138370 to your computer and use it in GitHub Desktop.
Save jrencz/8e6cb494ebf4d8138370 to your computer and use it in GitHub Desktop.
Quick sketch of Angular1 attribute directive that calls a function after a promise returned by ngClick handler is resolved or rejected
(function () {
'use strict';
angular
.module('finally', [])
.directive('finally', function (
$parse
) {
return {
restrict: 'A',
priority: 1,
terminal: true,
link(scope, element, attr) {
const clickHandler = angular.isFunction(attr.ngClick) ?
attr.ngClick :
$parse(attr.ngClick);
element.on('click', $event => {
scope.$apply(() => {
const promise = clickHandler(scope, {$event});
if (promise && 'finally' in promise) {
promise.finally(() => {
scope.$eval(attr.finally);
});
}
});
});
},
};
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment