This article focuses on Angular directives — what are they, how to use them, and to build our own.
Directives are perhaps the most important bit of an Angular application, and if we think about it, the most-used Angular unit, the component, is actually a directive.
An Angular component isn't more than a directive with a template. When we say that components are the building blocks of Angular applications, we’re actually saying that directives are the building blocks of Angular applications.
Basic overview
At the core, a directive is a function that executes whenever the Angular compiler finds it in the DOM. Angular directives are used to extend the power of the HTML by giving it new syntax. Each directive has a name — either one from the Angular predefined like ng-repeat
, or a custom one which can be called anything. And each directive determines where it can be used: in an element
, attribute
, class
or comment
.
By default, from Angular versions 2 and onward, Angular directives are separated into three different types:
Components
As we saw earlier, components are just directives with templates. Under the hood, they use the directive API and give us a cleaner way to define them.
The other two directive types don't have templates. Instead, they’re specifically tailored to DOM manipulation.
Attribute directives
Attribute directives manipulate the DOM by changing its behavior and appearance.
We use attribute directives to apply conditional style to elements, show or hide elements or dynamically change the behavior of a component according to a changing property.
Structural directives
These are specifically tailored to create and destroy DOM elements.
Some attribute directives — like hidden
, which shows or hides an element — basically maintain the DOM as it is. But the structural Angular directives are much less DOM friendly, as they add or completely remove elements from the DOM. So, when using these, we have to be extra careful, since we’re actually changing the HTML structure.
Using the Existing Angular Directives
Using the existing directives in Angular is fairly easy, and if you've written an Angular application in the past, I'm pretty sure you've used them. The ngClass
directive is a good example of an existing Angular attribute directive:
<p [ngClass]="{'blue'=true, 'yellow'=false}">
Angular Directives Are Cool!
</p>
<style>
.blue{color: blue}
.yellow{color: yellow}
</style>
So, by using the ngClass
directive on the example below, we’re actually adding the blue
class to our paragraph, and explicitly not adding the yellow
one. Since we’re changing the appearance of a class, and not changing the actual HTML structure, this is clearly an attribute directive. But Angular also offers out-of-the-box structural directives, like the ngIf
:
@Component({
selector: 'ng-if-simple',
template: `
<button (click)="show = !show"></button>
show =
<br>
<div *ngIf="show">Text to show</div>
`
})
class NgIfSimple {
show: boolean = true;
}
In this example, we use the ngIf
directive to add or remove the text using a button. In this case, the HTML structure itself is affected, so it's clearly a structural directive.
For a complete list of available Angular directives, we can check the official documentation.
As we saw, using Angular directives is quite simple. The real power of Angular directives comes with the ability to create our own. Angular provides a clean and simple API for creating custom directives, and that's what we’ll be looking at in the following sections.
Continue reading %A Practical Guide to Angular Directives%
by Claudio Ribeiro via SitePoint
No comments:
Post a Comment