Professional Documents
Culture Documents
One of the most powerful features Angular provides is directives. Directives extend the
behavior of HTML, enabling you to create custom HTML elements, attributes, and
classes with functionality specific to an application.
Angular provides many built-in directives, which provide the capability to interact with
form elements, bind data in a component to the view, and interact with browser events.
Directives are a combination of Angular template mark-up and supporting Typescript
code.
Angular directive markups can be HTML attributes, element names, or CSS classes.
The TypeScript directive code defines the template data and behavior of the HTML
elements.
The Angular compiler traverses the template DOM and compiles all directives.
Thenit links the directives by combining a directive with a scope to produce a new live
view. The live view contains the DOM elements and functionality defined in the
directive.
Built-in Directives
Much of the Angular functionality that you need to implement in HTML elements is
provided through built-in directives.
These directives provide a wide variety of support for Angular applications.
The following sections describe most of the Angular directives, which fall into three
categories:
1. Component: A directive with a template
2. Structural: A directive that manipulates elements in the DOM
3. Attribute: A directive that manipulates the appearance and behavior of a DOM
Element.
Components Directives
Angular components are a form of structural directive that utilize a template. A
component creates a selector that is used as an HTML tag to dynamically add HTML, CSS,
and Angular logic to the DOM. Components are at the heart of Angular.
Structural Directives
Several directives dynamically update, create, and remove elements from the DOM.
pg. 1
These directives create the layout, look, and feel of an application.
Directive Description
ngFor This directive is used to create a copy of a template for
each item within an iterable object. Here is an example:
<div *ngFor="let person of people"></div
ngIf displays a section of HTML if a value or an expression returns true. ngIf uses the
* symbol to let Angular know it’s there. The following is an example of the syntax for
ngIf:
<div *ngIf="myFunction(val)" >...</div>
<div *ngIf="myValue" >{{myValue}}</div>
ngSwitch uses ngSwitchCase, which displays a section of HTML if a value or an
expression returns true. ngSwitch is surrounded by [] as a form of one-waydata
binding to pass the data to each ngSwitchCase for evaluation. The following is an
example of the syntax for ngSwitch:
<div [ngSwitch]="time">
<span *ngSwitchCase="'night'">It's night time </span>
<span *ngSwitchDefault>It's day time </span>
pg. 2
Attribute Directives
Angular attribute directives modify how HTML elements look and behave.
They are injected straight into the HTML and dynamically modify how the user
interacts with an HTML segment. Attribute directives are so named because they
often look like normal HTML attributes.
An example of an attribute directive that you’ve been using throughout the book is
ngModel, which modifies an element by changing the display value.
Directive Description
ngModel This directive watches a variable for changes
and then updates display values
based on those changes. Consider these
examples:
<input [(ngModel)]="text"><br>
<h1>{{text}}</h1>
ngForm This directive creates a form group and
allows it to track the values and
validation within that form group. By using
ngSubmit, you can pass the
form data as an object to the submission
event. Here is an example:
<form #formName="ngForm"
(ngSubmit)="onSubmit(formName)"> </form
pg. 3
Normally, you will not use ng-init. You will use a controller or module instead.
Example:
<html>
<head>
<title>AngularJS First Application</title>
</head>
<body>
<h1>Sample Application</h1>
</body>
</html>
pg. 4