How to implement a directive that manages focus on dynamically added elements

0 votes
can someone help me with this by including relevant code or example,How to implement a directive that manages focus on dynamically added elements?
2 days ago in Angular by Nidhi
• 14,600 points
29 views

1 answer to this question.

0 votes

To implement a directive that manages focus on dynamically added elements in Angular, use ElementRef and AfterViewInit to set focus when the element is rendered.

Auto-Focus Directive

1. Create the Directive

import { Directive, ElementRef, AfterViewInit } from '@angular/core';

@Directive({

  selector: '[appAutoFocus]'

})

export class AutoFocusDirective implements AfterViewInit {

  constructor(private el: ElementRef) {}

  ngAfterViewInit() {

    this.el.nativeElement.focus();

  }

}

2. Use in Template

<input type="text" appAutoFocus />

answered 2 days ago by anonymous

Related Questions In Angular

0 votes
1 answer

How to implement a directive that adds tooltips to form controls dynamically?

Custom Tooltip Directive 1. Create the Directive import { ...READ MORE

answered 2 days ago in Angular by anonymous
18 views
0 votes
1 answer

How to develop a directive that modifies element styles based on form validation status?

Here's a concise directive that automatically applies ...READ MORE

answered 2 days ago in Angular by anonymous
18 views
0 votes
1 answer

How to create a custom directive that integrates with external charting libraries?

Compact Directive for External Chart Integration import { ...READ MORE

answered 2 days ago in Angular by anonymous
21 views
0 votes
1 answer
0 votes
0 answers

How do Observables improve API call handling in Angular?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
106 views
0 votes
0 answers

What’s the difference between Observables and Promises?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
76 views
0 votes
0 answers

What type of operation do RxJS operators allow for observables?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
85 views
0 votes
0 answers

When to use switchMap vs concatMap?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
111 views
0 votes
1 answer

How to build a directive that listens for specific keyboard shortcuts?

You can use the @HostListener decorator to ...READ MORE

answered 2 days ago in Angular by anonymous
39 views
0 votes
1 answer

How to develop a pipe that formats phone numbers based on locale?

You can use the libphonenumber-js library for ...READ MORE

answered 2 days ago in Angular by anonymous
23 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP