यह मेरे वर्तमान कोड है:
import { Directive, ElementRef, Input } from "@angular/core";
@Directive({
selector: "[autofocus]"
})
export class AutofocusDirective
{
private focus = true;
constructor(private el: ElementRef)
{
}
ngOnInit()
{
if (this.focus)
{
//Otherwise Angular throws error: Expression has changed after it was checked.
window.setTimeout(() =>
{
this.el.nativeElement.focus(); //For SSR (server side rendering) this is not safe. Use: https://github.com/angular/angular/issues/15008#issuecomment-285141070)
});
}
}
@Input() set autofocus(condition: boolean)
{
this.focus = condition !== false;
}
}
उपयोग के मामले:
[autofocus] //will focus
[autofocus]="true" //will focus
[autofocus]="false" //will not focus
पुराने कोड (पुराने जवाब, बस मामले में):
मैं इस कोड के साथ समाप्त हो गया:
import {Directive, ElementRef, Renderer} from '@angular/core';
@Directive({
selector: '[autofocus]'
})
export class Autofocus
{
constructor(private el: ElementRef, private renderer: Renderer)
{
}
ngOnInit()
{
}
ngAfterViewInit()
{
this.renderer.invokeElementMethod(this.el.nativeElement, 'focus', []);
}
}
यदि मैं कोड डालता हूं ngOnViewInit
में यह काम नहीं करता है। कोड भी सर्वोत्तम प्रथाओं का उपयोग करता है, क्योंकि तत्व पर ध्यान केंद्रित करना सीधे recommended नहीं है।
संपादित (सशर्त ऑटोफोकस):
कुछ दिन पहले मैं, सशर्त ऑटो फोकस की जरूरत है क्योंकि मैं पहले ऑटोफोकस तत्व छिपाने के लिए और मैं एक और ध्यान केंद्रित करना चाहते हैं, लेकिन केवल जब पहली दिखाई नहीं देता है, और मैं इस के साथ समाप्त हुआ कोड:
import { Directive, ElementRef, Renderer, Input } from '@angular/core';
@Directive({
selector: '[autofocus]'
})
export class AutofocusDirective
{
private _autofocus;
constructor(private el: ElementRef, private renderer: Renderer)
{
}
ngOnInit()
{
}
ngAfterViewInit()
{
if (this._autofocus || typeof this._autofocus === "undefined")
this.renderer.invokeElementMethod(this.el.nativeElement, 'focus', []);
}
@Input() set autofocus(condition: boolean)
{
this._autofocus = condition != false;
}
}
Edited2:
Renderer.invokeElementMethod is deprecated और नए Renderer2 इसका समर्थन नहीं करता। तो हम मूल फोकस पर वापस आ गए हैं (जो उदाहरण के लिए डीओएम - एसएसआर के बाहर काम नहीं करता है!)।
import { Directive, ElementRef, Input } from '@angular/core';
@Directive({
selector: '[autofocus]'
})
export class AutofocusDirective
{
private _autofocus;
constructor(private el: ElementRef)
{
}
ngOnInit()
{
if (this._autofocus || typeof this._autofocus === "undefined")
this.el.nativeElement.focus(); //For SSR (server side rendering) this is not safe. Use: https://github.com/angular/angular/issues/15008#issuecomment-285141070)
}
@Input() set autofocus(condition: boolean)
{
this._autofocus = condition != false;
}
}
उपयोग के मामले:
<md-input-container>
<input mdInput placeholder="Item Id" formControlName="itemId" name="itemId" myAutofocus>
</md-input-container>
का विकल्प:
[autofocus] //will focus
[autofocus]="true" //will focus
[autofocus]="false" //will not focus
दूसरा विकल्प सीधे बॉक्स से बाहर काम करना चाहिए। यह नहीं है? – Hampus
नहीं, यह नहीं है। – Makla
बस "ऑटोफोकस" का उपयोग करना काम नहीं करता है क्योंकि पेज लोड होने पर यह केवल प्रभावी होता है, न कि जब कोणीय सामग्री को अंदर और बाहर स्वैप कर रहा है। – Dave