Get element of nonpermanent nested child component
up vote
1
down vote
favorite
I have a parent Component, that after some actions will call a child component after an ngIf returns true, and this child component has an Input element that I want to focus. Because the child component is not instantiated when the parent is, using ViewChild doesn't seem to be working for me. I am getting close to a good strategy to focus this input with a service, but actually managing to get a reference to this input when needed I am finding to be difficult.
angular angular5 autofocus
add a comment |
up vote
1
down vote
favorite
I have a parent Component, that after some actions will call a child component after an ngIf returns true, and this child component has an Input element that I want to focus. Because the child component is not instantiated when the parent is, using ViewChild doesn't seem to be working for me. I am getting close to a good strategy to focus this input with a service, but actually managing to get a reference to this input when needed I am finding to be difficult.
angular angular5 autofocus
1
Did you try focusing on the input element onInit() of the child component?
– Abhidev
Nov 13 at 20:21
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a parent Component, that after some actions will call a child component after an ngIf returns true, and this child component has an Input element that I want to focus. Because the child component is not instantiated when the parent is, using ViewChild doesn't seem to be working for me. I am getting close to a good strategy to focus this input with a service, but actually managing to get a reference to this input when needed I am finding to be difficult.
angular angular5 autofocus
I have a parent Component, that after some actions will call a child component after an ngIf returns true, and this child component has an Input element that I want to focus. Because the child component is not instantiated when the parent is, using ViewChild doesn't seem to be working for me. I am getting close to a good strategy to focus this input with a service, but actually managing to get a reference to this input when needed I am finding to be difficult.
angular angular5 autofocus
angular angular5 autofocus
asked Nov 13 at 20:18
Melvin Gruschow
113
113
1
Did you try focusing on the input element onInit() of the child component?
– Abhidev
Nov 13 at 20:21
add a comment |
1
Did you try focusing on the input element onInit() of the child component?
– Abhidev
Nov 13 at 20:21
1
1
Did you try focusing on the input element onInit() of the child component?
– Abhidev
Nov 13 at 20:21
Did you try focusing on the input element onInit() of the child component?
– Abhidev
Nov 13 at 20:21
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can try setting the autofocus attribute of the input.
<input ... autofocus="true">
If that doesn't work, then you can create a directive that will force the input to receive focus after the DOM element is created. Here is a directive that I've been using and it has the option to delay the setting of the focus by JS digests which is handy for some difficult component arrangements.
@Directive({
selector: '[ui-focus]'
})
export class FocusDirective implements AfterViewInit {
/**
* The number of JavaScript digestions to complete before setting focus.
*/
@Input()
public focusDelay: string | boolean | number = 0;
/**
* Conditionally control if focus should be set.
*/
@Input()
public focusEnabled: boolean = true;
private counter: number;
public constructor(private el: ElementRef) {
}
public ngAfterViewInit(): void {
if (Boolean(this.focusEnabled)) {
this.counter = this.focusDelay === true ? 1 : parseInt(this.focusDelay + '', 10);
this.setFocus();
}
}
private setFocus() {
if (this.counter <= 0) {
(<HTMLElement>this.el.nativeElement).focus();
} else {
this.counter--;
window.setTimeout(() => this.setFocus());
}
}
}
You can then assign the directive to the input control.
<input ... ui-focus>
You shouldn't have to do anything more complicated than this (i.e. no need for a service).
I have tried out the autofocus property, but the situation is slightly more complicated in that this input can appear and reappear multiple times, and autofocus does not seem to work after the first instance. I will give some of these other tricks a try and see.
– Melvin Gruschow
Nov 13 at 20:39
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can try setting the autofocus attribute of the input.
<input ... autofocus="true">
If that doesn't work, then you can create a directive that will force the input to receive focus after the DOM element is created. Here is a directive that I've been using and it has the option to delay the setting of the focus by JS digests which is handy for some difficult component arrangements.
@Directive({
selector: '[ui-focus]'
})
export class FocusDirective implements AfterViewInit {
/**
* The number of JavaScript digestions to complete before setting focus.
*/
@Input()
public focusDelay: string | boolean | number = 0;
/**
* Conditionally control if focus should be set.
*/
@Input()
public focusEnabled: boolean = true;
private counter: number;
public constructor(private el: ElementRef) {
}
public ngAfterViewInit(): void {
if (Boolean(this.focusEnabled)) {
this.counter = this.focusDelay === true ? 1 : parseInt(this.focusDelay + '', 10);
this.setFocus();
}
}
private setFocus() {
if (this.counter <= 0) {
(<HTMLElement>this.el.nativeElement).focus();
} else {
this.counter--;
window.setTimeout(() => this.setFocus());
}
}
}
You can then assign the directive to the input control.
<input ... ui-focus>
You shouldn't have to do anything more complicated than this (i.e. no need for a service).
I have tried out the autofocus property, but the situation is slightly more complicated in that this input can appear and reappear multiple times, and autofocus does not seem to work after the first instance. I will give some of these other tricks a try and see.
– Melvin Gruschow
Nov 13 at 20:39
add a comment |
up vote
0
down vote
You can try setting the autofocus attribute of the input.
<input ... autofocus="true">
If that doesn't work, then you can create a directive that will force the input to receive focus after the DOM element is created. Here is a directive that I've been using and it has the option to delay the setting of the focus by JS digests which is handy for some difficult component arrangements.
@Directive({
selector: '[ui-focus]'
})
export class FocusDirective implements AfterViewInit {
/**
* The number of JavaScript digestions to complete before setting focus.
*/
@Input()
public focusDelay: string | boolean | number = 0;
/**
* Conditionally control if focus should be set.
*/
@Input()
public focusEnabled: boolean = true;
private counter: number;
public constructor(private el: ElementRef) {
}
public ngAfterViewInit(): void {
if (Boolean(this.focusEnabled)) {
this.counter = this.focusDelay === true ? 1 : parseInt(this.focusDelay + '', 10);
this.setFocus();
}
}
private setFocus() {
if (this.counter <= 0) {
(<HTMLElement>this.el.nativeElement).focus();
} else {
this.counter--;
window.setTimeout(() => this.setFocus());
}
}
}
You can then assign the directive to the input control.
<input ... ui-focus>
You shouldn't have to do anything more complicated than this (i.e. no need for a service).
I have tried out the autofocus property, but the situation is slightly more complicated in that this input can appear and reappear multiple times, and autofocus does not seem to work after the first instance. I will give some of these other tricks a try and see.
– Melvin Gruschow
Nov 13 at 20:39
add a comment |
up vote
0
down vote
up vote
0
down vote
You can try setting the autofocus attribute of the input.
<input ... autofocus="true">
If that doesn't work, then you can create a directive that will force the input to receive focus after the DOM element is created. Here is a directive that I've been using and it has the option to delay the setting of the focus by JS digests which is handy for some difficult component arrangements.
@Directive({
selector: '[ui-focus]'
})
export class FocusDirective implements AfterViewInit {
/**
* The number of JavaScript digestions to complete before setting focus.
*/
@Input()
public focusDelay: string | boolean | number = 0;
/**
* Conditionally control if focus should be set.
*/
@Input()
public focusEnabled: boolean = true;
private counter: number;
public constructor(private el: ElementRef) {
}
public ngAfterViewInit(): void {
if (Boolean(this.focusEnabled)) {
this.counter = this.focusDelay === true ? 1 : parseInt(this.focusDelay + '', 10);
this.setFocus();
}
}
private setFocus() {
if (this.counter <= 0) {
(<HTMLElement>this.el.nativeElement).focus();
} else {
this.counter--;
window.setTimeout(() => this.setFocus());
}
}
}
You can then assign the directive to the input control.
<input ... ui-focus>
You shouldn't have to do anything more complicated than this (i.e. no need for a service).
You can try setting the autofocus attribute of the input.
<input ... autofocus="true">
If that doesn't work, then you can create a directive that will force the input to receive focus after the DOM element is created. Here is a directive that I've been using and it has the option to delay the setting of the focus by JS digests which is handy for some difficult component arrangements.
@Directive({
selector: '[ui-focus]'
})
export class FocusDirective implements AfterViewInit {
/**
* The number of JavaScript digestions to complete before setting focus.
*/
@Input()
public focusDelay: string | boolean | number = 0;
/**
* Conditionally control if focus should be set.
*/
@Input()
public focusEnabled: boolean = true;
private counter: number;
public constructor(private el: ElementRef) {
}
public ngAfterViewInit(): void {
if (Boolean(this.focusEnabled)) {
this.counter = this.focusDelay === true ? 1 : parseInt(this.focusDelay + '', 10);
this.setFocus();
}
}
private setFocus() {
if (this.counter <= 0) {
(<HTMLElement>this.el.nativeElement).focus();
} else {
this.counter--;
window.setTimeout(() => this.setFocus());
}
}
}
You can then assign the directive to the input control.
<input ... ui-focus>
You shouldn't have to do anything more complicated than this (i.e. no need for a service).
answered Nov 13 at 20:33
cgTag
21.9k758107
21.9k758107
I have tried out the autofocus property, but the situation is slightly more complicated in that this input can appear and reappear multiple times, and autofocus does not seem to work after the first instance. I will give some of these other tricks a try and see.
– Melvin Gruschow
Nov 13 at 20:39
add a comment |
I have tried out the autofocus property, but the situation is slightly more complicated in that this input can appear and reappear multiple times, and autofocus does not seem to work after the first instance. I will give some of these other tricks a try and see.
– Melvin Gruschow
Nov 13 at 20:39
I have tried out the autofocus property, but the situation is slightly more complicated in that this input can appear and reappear multiple times, and autofocus does not seem to work after the first instance. I will give some of these other tricks a try and see.
– Melvin Gruschow
Nov 13 at 20:39
I have tried out the autofocus property, but the situation is slightly more complicated in that this input can appear and reappear multiple times, and autofocus does not seem to work after the first instance. I will give some of these other tricks a try and see.
– Melvin Gruschow
Nov 13 at 20:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288872%2fget-element-of-nonpermanent-nested-child-component%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Did you try focusing on the input element onInit() of the child component?
– Abhidev
Nov 13 at 20:21