Skip to content

Instantly share code, notes, and snippets.

@Meligy
Last active December 16, 2015 08:28
Show Gist options
  • Save Meligy/5405539 to your computer and use it in GitHub Desktop.
Save Meligy/5405539 to your computer and use it in GitHub Desktop.
Sample directive to set default button click using Angular JS in ASP.NET webforms app
var app = angular.module("app", []);
app = app.directive('appDefaultButton', function () {
return function (scope, element, attr, ctrl) {
// This is all standard directive code copied from ng-sanitize
// The point of it is store & track
// if passed value was changed & re-run
element.addClass('app-default-button')
.data('$appDefaultButton', attr.appDefaultButton);
scope.$watch(attr.appDefaultButton,
function appDefaultButtonWatchAction(value) {
if (!document || !document.forms) {
return;
}
document.forms[0].action =
// instead of URL, run some JS
// if we don't do "void()",
// Firefox replaces all page with return
'javascript:void(' +
// wrap the button with angular, and trigger click
'angular.element(document.getElementById("' +
value + '"))'+ '.triggerHandler("click")' +
');'; // closing the "void(" part
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment