give_focus.ts 643 B

1234567891011121314151617181920212223242526272829
  1. import coreModule from './core_module';
  2. coreModule.directive('giveFocus', () => {
  3. return (scope: any, element: any, attrs: any) => {
  4. element.click((e: any) => {
  5. e.stopPropagation();
  6. });
  7. scope.$watch(
  8. attrs.giveFocus,
  9. (newValue: any) => {
  10. if (!newValue) {
  11. return;
  12. }
  13. setTimeout(() => {
  14. element.focus();
  15. const domEl: any = element[0];
  16. if (domEl.setSelectionRange) {
  17. const pos = element.val().length * 2;
  18. domEl.setSelectionRange(pos, pos);
  19. }
  20. }, 200);
  21. },
  22. true
  23. );
  24. };
  25. });
  26. export default {};