Skip to content

Instantly share code, notes, and snippets.

@aeinbu
Last active February 10, 2017 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeinbu/39913d59aeb4d1760b8dab700cc34169 to your computer and use it in GitHub Desktop.
Save aeinbu/39913d59aeb4d1760b8dab700cc34169 to your computer and use it in GitHub Desktop.
const angular = require("angular");
angular
.module("modulename")
.directive('attachFocusFunction', function($parse) {
return {
scope: false,
link(scope, element, attributes) {
let expr = $parse(attributes.attachFocusFunction);
if(!expr.assign){
throw "Please provide a valid lhs expression for the attachFocusFunction";
}
expr.assign(scope, function() {
element[0].focus();
});
}
}
});
@aeinbu
Copy link
Author

aeinbu commented Jan 4, 2017

<!-- in component's html template -->
<input type="text" attach-focus-function="$ctrl.setFocus" />
// in component's controller
this.setFocus();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment