Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import {Directive, Input, ElementRef} from 'angular2/core';
  2. @Directive({
  3. selector: '[focus]'
  4. })
  5. class FocusDirective {
  6. @Input()
  7. focus:boolean;
  8. constructor(@Inject(ElementRef) private element: ElementRef) {}
  9. protected ngOnChanges() {
  10. this.element.nativeElement.focus();
  11. }
  12. }
  13.  
  14. // Usage
  15. @Component({
  16. selector : 'app',
  17. template : `
  18. <input [focus]="inputFocused" type="text">
  19. <button (click)="moveFocus()">Move Focus</button>
  20. `,
  21. directives: [FocusDirective]
  22. })
  23. export class App {
  24. private inputFocused = false;
  25. moveFocus() {
  26. this.inputFocused = true;
  27. // we need this because nothing will
  28. // happens on next method call,
  29. // ngOnChanges in directive is only called if value is changed,
  30. // so we have to reset this value in async way,
  31. // this is ugly but works
  32. setTimeout(() => {this.inputFocused = false});
  33. }
  34. }
  35.  
  36. import { Directive, ElementRef, Renderer, AfterViewInit } from '@angular/core';
  37. @Directive({
  38. selector: '[focus]'
  39. })
  40.  
  41. export class DmFocusDirective implements AfterViewInit {
  42. constructor(private _el: ElementRef, private renderer: Renderer) {
  43. }
  44.  
  45. ngAfterViewInit() {
  46. this.renderer.invokeElementMethod(this._el.nativeElement, 'focus');
  47. }
  48. }
  49.  
  50. this.renderer.invokeElementMethod(this.input.nativeElement, 'focus', []);
  51.  
  52. import { Directive, ElementRef, Renderer} from "@angular/core";
  53. @Directive({
  54. selector: "[Focus]"
  55. })
  56.  
  57. export class myFocus {
  58. constructor(private _el: ElementRef, private renderer: Renderer) {
  59. this.renderer.invokeElementMethod(this._el.nativeElement, 'focus');
  60. }
  61.  
  62. }
  63.  
  64. import {Directive, AfterViewInit, ElementRef, DoCheck} from '@angular/core';
  65.  
  66. @Directive({selector: '[focus]'})
  67. export class FocusDirective implements AfterViewInit, DoCheck {
  68. private lastVisible: boolean = false;
  69. private initialised: boolean = false;
  70.  
  71. constructor(private el: ElementRef) {
  72. }
  73.  
  74. ngAfterViewInit() {
  75. console.log('inside FocusDirective.ngAfterViewInit()');
  76. this.initialised = true;
  77. this.ngDoCheck();
  78. }
  79.  
  80. ngDoCheck() {
  81.  
  82. console.log('inside FocusDirective.ngDoCheck()');
  83.  
  84. if (!this.initialised) {
  85. return;
  86. }
  87. const visible = !!this.el.nativeElement.offsetParent;
  88. if (visible && !this.lastVisible) {
  89. setTimeout(() => {
  90. this.el.nativeElement.focus();
  91. }, 1);
  92. }
  93. this.lastVisible = visible;
  94. }
  95. }
  96.  
  97. const element = this.renderer.selectRootElement('#elementId');
  98.  
  99. element.focus();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement