Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Events
Videos
Audiobooks
Packt Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Angular 2 Components

You're reading from   Angular 2 Components Practical and easy-to-follow guide to Angular 2 Components

Arrow left icon
Product type Paperback
Published in Nov 2016
Publisher Packt
ISBN-13 9781785882340
Length 124 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Nir Kaufman Nir Kaufman
Author Profile Icon Nir Kaufman
Nir Kaufman
Thierry Templier Thierry Thierry Templier Thierry
Author Profile Icon Thierry Templier Thierry
Thierry Templier Thierry
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Angular 2 Component Architecture FREE CHAPTER 2. Setting Up an Angular 2 Development Environment with angular-cli 3. The TypeScript Primer 4. Building a Basic Component 5. Building Dynamic Components 6. Component Communication 7. Putting It All Together 8. Integrating Third-Party Components 9. Angular 2 Directives Index

The basic structural directive

As we mentioned at the beginning of this chapter, the third type of directive is called structural directives, and as the name suggests, those directives are meant to manipulate the element that they applied on. Angular core includes several directives that manipulate the DOM, such as ngIf, ngFor, and ngSwitch.

For our example, we will implement our own ngIf directive that behaves just like the original one.

First, create a new file named only-if.ts and let's define the basic structure for our directive:

[only-if.ts]
import { Directive } from '@angular/core';

@Directive({
  selector: '[onlyIf]'
})
export class OnlyIf {
}

The structural directives begin their lives just like an attribute directive. We import the @Directive decorator from the Angular core and declare the selector as an attribute.

Next, we will need to access the template, and we will need some kinds of container so we can attach or remove views. For this, we will need to...

lock icon The rest of the chapter is locked
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Angular 2 Components
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime
Modal Close icon
Modal Close icon