Please check this method, https://gist.github.com/EpokK/5884263
You can simply create the ng-enter directive and pass your action as a paramater
app.directive('ngEnter', function() { return function(scope, element, attrs) { element.bind("keydown keypress", function(event) { if(event.which === 13) { scope.$apply(function(){ scope.$eval(attrs.ngEnter); }); event.preventDefault(); } }); }; });
HTML
<input ng-enter="myControllerFunction()" />
You can change the name ng-enter to something else, because ng-** reserved by the main Angular command.
I also see that your controller is dealing with the DOM, and you shouldn't. Move this logic to another directive or to HTML, and keep the controller worse.
if (ts.test.userTestId) { btn = document.getElementById('viewQuestions'); //NOT in controller } else { btn = document.getElementById('acquireTest'); //NOT in controller } $timeout(function () { btn.focus(); //NOT in controller btn.click(); //NOT in controller window.setTimeout(function () { // $timeout in $timeout, questionable btn.blur(); //NOT in controller }, 500); })
source share