Can we create a custom directive in Angular

0 votes
With the help of code and example tell me Can we create a custom directive in Angular?
Feb 26 in Angular by Nidhi
• 10,860 points
35 views

1 answer to this question.

0 votes

You can create a custom directive in Angular using the @Directive decorator.

Example: Custom Highlight Directive

1. Create the Directive (highlight.directive.ts)

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

@Directive({

  selector: '[appHighlight]'

})

export class HighlightDirective {

  constructor(private el: ElementRef) {}

  @HostListener('mouseenter') onMouseEnter() {

    this.el.nativeElement.style.backgroundColor = 'yellow';

  }

  @HostListener('mouseleave') onMouseLeave() {

    this.el.nativeElement.style.backgroundColor = 'transparent';

  }

}

2. Use in a Component

<p appHighlight>Hover over me to highlight!</p>

answered Feb 26 by Tanya

Related Questions In Angular

0 votes
1 answer

How to know tools and bundlers after create a new workspace or a project in angular?

Hello @sajal, When you create projects and workspaces ...READ MORE

answered Aug 6, 2020 in Angular by Niroj
• 82,840 points
1,138 views
0 votes
0 answers

How can we combine multiple built-in pipes in Angular templates?

Can you help me with a code ...READ MORE

6 days ago in Angular by Nidhi
• 10,860 points
27 views
0 votes
0 answers

Can we apply two pipes together in Angular?

I doubt it. Can we apply two ...READ MORE

6 days ago in Angular by Nidhi
• 10,860 points
26 views
+1 vote
8 answers

How can I implement process.env in Angular 5 environment?

Users do not have access to process.env ...READ MORE

answered Apr 3, 2018 in DevOps & Agile by DareDev
• 6,890 points
13,452 views
0 votes
1 answer
0 votes
4 answers

ReactJS vs Angular Comparison: Which is better?

Parameters React Angular Type React is a JavaScript library, and it ...READ MORE

answered Jan 7, 2021 in Events & Trending Topics by Focusteck
• 140 points
2,046 views
+4 votes
9 answers

***IMPORTANT*** AngularJS Interview Questions.

Yes, I agree with Omkar AngularJs is ...READ MORE

answered Mar 17, 2019 in Career Counselling by Sharad
• 180 points
3,940 views
0 votes
1 answer

How can we add the style conditionally in Angular?

In Angular, you can add styles conditionally ...READ MORE

answered Feb 26 in Angular by Tanya
51 views
0 votes
1 answer

How can structural directives manipulate the DOM in Angular?

Structural directives in Angular (*ngIf, *ngFor, *ngSwitch) ...READ MORE

answered Feb 26 in Angular by Navya
40 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