Skip to content

Instantly share code, notes, and snippets.

@kenziebottoms
Created May 1, 2018 19:06
Show Gist options
  • Save kenziebottoms/d5c0e3e0f8fac33e33f02b8530f744ae to your computer and use it in GitHub Desktop.
Save kenziebottoms/d5c0e3e0f8fac33e33f02b8530f744ae to your computer and use it in GitHub Desktop.
Angular directive: `ng-enter`
"use strict";
const angular = require("angular");
// reference: https://stackoverflow.com/questions/17470790/how-to-use-a-keypress-event-in-angularjs
angular.module("mixtape").directive("ngEnter", function () {
return {
controller: "MixCtrl",
link: function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if (event.which === 13) {
scope.$apply(function() {
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment