Directives Flashcards

1
Q

What is a Directive

A

It is a function that executes when Angular finds it in DOM

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Examples of Directives

A

ngRepeat, ngIf, ngFor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Types of Directives

A

Structural and Attribute

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Structural Directive

A

Create or destroy DOM elements
hide, show, enable, disable
ngIf, ngFor, ngSwitch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Attribute Directive

A

Change the state or behavior of DOM elements, component or another directive
ngClass - Add or remove several css classes simultaneously
ngStyle - set multiple inline styles simultaneously
ngModel - display data and update data when user makes changes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

[ngClass]=”{‘blue’=true, ‘yellow’=false}” - Attribute Directive

A
Adds the class blue and removes class yellow if present.
To add or remove a single class use class binding rather than ngClass
[class.sale] = "onSale" like boolean/undefined/null
[class]="classExpression" like string, object, array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

{{show ? ‘hide’ : ‘show’}}
show = {{show}}
<br></br>
<div>Text to show</div>

A

It changes the structure of DOM tree and hence it is Structural Directive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Custom Directive

A

First import {Directive, ElementRef} from ‘@angular/core’;
@Directive({
selector:’[my-error]’
})

export class MyErrorDirective{
    constructor(elr:ElementRef){
        elr.nativeElement.style.background='red';
    }
}

Now this directive can be used in any component

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How is the custom directive used?

A

After creating the directive include it in app.module.ts file in @NgModule–> declarations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can modules have their own attribute directives

A

Yes, RouteModule, FormModule has their own attribute directives.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

[(ngModel)]

A

Two way data binding
It is part of FormsModule (include this in app component)

without ngModel example:

with ngModel example:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Useful site

A

https://www.sitepoint.com/practical-guide-angular-directives/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When to use ngStyle directive?

A

Most styles can be added simply to the HTML directly, for those we can simply add them to the class property, and no special Angular functionality is needed.

Many state styles can be used using browser-supported pseudo-class selectors such as :focus, we should prefer those conventional solutions for those cases.

For state styles that don’t have a pseudo-class selector linked to it, its best to go with ngClass.

If the ngClass expressions get too big, it’s a good idea to move the calculation of the styles to the component class.

Only for situations where we have a dynamically calculated embedded style should we use ngStyle, this should be rarely needed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly