Last active
August 29, 2015 14:10
-
-
Save markthiessen/94018112038cb60f7e06 to your computer and use it in GitHub Desktop.
AngularJS (+Bootstrap) directive for inline ngClick confirmation
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
(function () { | |
'use strict'; | |
angular.module('yourModule').directive('inlineConfirm', | |
['$compile', | |
function ($compile) { | |
return { | |
priority: -100, | |
link: function (scope, elm, attrs) { | |
var wrapperTemplate = '<div class="popover inline-confirm" role="tooltip"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'; | |
var template = | |
'<div>' | |
+ '<button type="button" class="btn btn-danger" ng-click="confirm()">Yes</button> ' | |
+ '<button type="button" class="btn btn-default" ng-click="cancel()">No, keep it</button>' | |
+ '</div>'; | |
elm.bind('click', function (e) { | |
e.stopImmediatePropagation(); | |
e.preventDefault(); | |
var compiledTemplate = $compile(template)(scope); | |
elm.popover({ | |
animation: false, | |
template: wrapperTemplate, | |
title: attrs.inlineConfirm || 'Are you sure?', | |
//container: 'body', | |
html: true, | |
content: compiledTemplate | |
}); | |
elm.popover('show'); | |
}); | |
scope.confirm = function () { | |
scope.$eval(attrs.ngClick); | |
elm.popover('destroy'); | |
}; | |
scope.cancel = function () { | |
elm.popover('destroy'); | |
}; | |
scope.$on('$destroy', function () { | |
elm.popover('destroy'); | |
}); | |
} | |
}; | |
}]); | |
})(); | |
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
<a class="btn btn-danger" inline-confirm ng-click="removeTheThing();">Remove</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment