Created
May 1, 2018 19:06
-
-
Save kenziebottoms/d5c0e3e0f8fac33e33f02b8530f744ae to your computer and use it in GitHub Desktop.
Angular directive: `ng-enter`
This file contains 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
"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