Angular 2 Tutorial [4] - Template Directives

Traversy Media · Beginner ·🛠️ AI Tools & Apps ·10y ago

Key Takeaways

This video tutorial covers the use of Angular 2 template directives such as ngIf, ngSwitch, and ngFor, demonstrating how to implement conditional statements, switch cases, and loops in Angular 2 templates.

Full Transcript

Hey guys, in this video I'm going to show you how to use some of the built-in template directives, uh, ng if, ng switch, and ng4. All right, so what I have here is just a default quick start application. If you need to to get the quick start, you can get it from this GitHub page. You can download it or you can clone it. We cloned it in the second part of this series, so if you need to go back, you can do that. All right. So, we're going to just work in the the root app component that is uh included by default. And what we're going to do here is down in the class. Let's create a constructor. All right. And let's create a property. We'll say private. And we'll call this show heading. No, not that. What am I doing? All right. So, show heading. And then here we'll say this dot show heading. Set that to true. All right. And then up here in our template in the H1, we're going to use ng if. Now the syntax is a little different from Angular one. What you're going to want to do is use the asterisk and then say ng if and then set it to in this case show heading. All right. So, if we save that and we take a look, you can see that it's showing the heading. If I go down and I change this to false and save, now it's not. Okay. So, it's it's testing to see if show heading is true or false, which we're setting right here. All right. Very, very easy. So, next thing I want to show you is the switch statement. I'm just going to set that back to true. All right. So for the switch, what I'm going to do is let's create a property and we'll call it name. And then in here we'll say this.name equals John Doe. All right. Now up here in the template, what I'm going to do is use back ticks here so that we can use multiple lines. All right. And then let's see, we're going to put a span and we want to use our ng switch. Now, the ng switch is actually going to go in brackets. All right. And then we're going to set that equal to name. Now, inside of here, we're going to use more span tags. And this time, we're going to use an asterisk and say ng switch when and then let's set that. We're going to put single quotes inside the double quotes here because we're using a string. And let's say uh John Doe. All right. And then inside the span, we're going to say John Doe. Actually, let's just use first names. Say John there and there. And then down here, we'll just say John. All right. All right. And then I'm going to copy this. Okay. This one here we'll set to Sam. And this one we'll set to Jeff. All right. So, let's save that and run it. And you can see that we're getting John because what it's doing is it's checking name. All right. And it's saying when name equals John, then do this. spit out John. All right. So, if I go down here and I change this to Sam and save, now we get Sam. Now, what if we change to something that uh isn't even there. So, let's say Mike. All right. So, it's not showing Mike because that's not one of our cases. So, what we would usually do is use a default. All right. So, let's paste that in and then change this to switch default. And we can just get rid of this totally. And here, let's just say not here. Okay, we'll keep this set as Mike. And now you can see we're getting not here because it's it's default. It's not John, Sam, or Jeff. All right, so that's ng switch. The final one that I want to show you is ng4. And this is to uh to to create a loop. So what we'll do is create an array up here. Let's say private colors. And in here we'll say this dot colors equals red blue and green. Okay. So we have these colors and we want to output each one of these. So let's go up here and I'm going to put in a line break. Couple of them. And we're going to create an aul. And then we want to take the element that we want to repeat. So this is going to be an LI that we want to repeat. And then we're going to say asterisk ng4 and set that. We're going to use the number sign and color of colors. All right. And then in here we can say color. Okay. So you can put whatever you use here you can use here. And then this colors is coming from the property colors. So let's save that. And now if we look you can see that it's looping through the colors and outputting them on the screen. All right. So those are some of the template directives that you can use. In the next video, we're going to take a look at pipes.

Original Description

Updated Crash Course Using Version 2.2 Stable - https://www.youtube.com/watch?v=-zW1zHqsdyc&feature=youtu.be In this video we will look at template directives such as ngIf, ngSwitch and ngFor
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Traversy Media · Traversy Media · 0 of 60

← Previous Next →
1 Changing Your DNS/Nameservers
Changing Your DNS/Nameservers
Traversy Media
2 Create a MySQL database in cPanel
Create a MySQL database in cPanel
Traversy Media
3 Install & Uninstall Joomla Extensions
Install & Uninstall Joomla Extensions
Traversy Media
4 Adding and linking an article in Joomla
Adding and linking an article in Joomla
Traversy Media
5 Create a Joomla Blog
Create a Joomla Blog
Traversy Media
6 Import & Export A MySQL Database
Import & Export A MySQL Database
Traversy Media
7 Use A Custom Font On Your Website Using CSS
Use A Custom Font On Your Website Using CSS
Traversy Media
8 Connect Joomla Site With Dreamweaver
Connect Joomla Site With Dreamweaver
Traversy Media
9 Remove Phoca Gallery 3.2.3 Footer Text
Remove Phoca Gallery 3.2.3 Footer Text
Traversy Media
10 Drupal 7 Security Update 7.19 to 7.20
Drupal 7 Security Update 7.19 to 7.20
Traversy Media
11 Add An Addon Domain In Cpanel
Add An Addon Domain In Cpanel
Traversy Media
12 Pull A Heroku Rails App and Database
Pull A Heroku Rails App and Database
Traversy Media
13 Create a Custom Joomla 2.5 Module - Part 1
Create a Custom Joomla 2.5 Module - Part 1
Traversy Media
14 Create a Custom Joomla 2.5 Module - Part 2
Create a Custom Joomla 2.5 Module - Part 2
Traversy Media
15 Create a Custom Joomla 2.5 Module - Part 3
Create a Custom Joomla 2.5 Module - Part 3
Traversy Media
16 Joomla SEO Tutorial - sh404sef Configuration
Joomla SEO Tutorial - sh404sef Configuration
Traversy Media
17 Font Dragr
Font Dragr
Traversy Media
18 Convert an HTML Template to Joomla 2.5/3.0 - Part One
Convert an HTML Template to Joomla 2.5/3.0 - Part One
Traversy Media
19 Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Traversy Media
20 Rockettheme Rocketlauncher   Joomla Site in Under 10 Minutes
Rockettheme Rocketlauncher Joomla Site in Under 10 Minutes
Traversy Media
21 JQuery FAQ Slider Tutorial
JQuery FAQ Slider Tutorial
Traversy Media
22 301 Redirect With htaccess File
301 Redirect With htaccess File
Traversy Media
23 Convert HTML to Wordpress Theme - Part 1
Convert HTML to Wordpress Theme - Part 1
Traversy Media
24 Convert HTML to Wordpress Theme - Part 2
Convert HTML to Wordpress Theme - Part 2
Traversy Media
25 Easy JQuery Widgets
Easy JQuery Widgets
Traversy Media
26 Codeigniter App Part 1 - Creating the Database
Codeigniter App Part 1 - Creating the Database
Traversy Media
27 Codeigniter App Part 2 - Installation and Configuration
Codeigniter App Part 2 - Installation and Configuration
Traversy Media
28 Codeigniter App Part 6 - Login/Register System
Codeigniter App Part 6 - Login/Register System
Traversy Media
29 Codeigniter App Part 7 - Models List CRUD
Codeigniter App Part 7 - Models List CRUD
Traversy Media
30 Codeigniter App Part 8 - Models Task CRUD
Codeigniter App Part 8 - Models Task CRUD
Traversy Media
31 Node.js Part 1 - Install NodeJS on Windows
Node.js Part 1 - Install NodeJS on Windows
Traversy Media
32 Node.js Part 3 - Building a Static Page Server
Node.js Part 3 - Building a Static Page Server
Traversy Media
33 Node.js Part 4 - NPM
Node.js Part 4 - NPM
Traversy Media
34 Node.js Part 2 - Install MongoDB in Windows
Node.js Part 2 - Install MongoDB in Windows
Traversy Media
35 Create a Joomla Quickstart with Custom Sample Data
Create a Joomla Quickstart with Custom Sample Data
Traversy Media
36 Install MongoDB in Ubuntu
Install MongoDB in Ubuntu
Traversy Media
37 HTML5 Web Storage
HTML5 Web Storage
Traversy Media
38 Create a Joomla Bootstrap Template From Scratch
Create a Joomla Bootstrap Template From Scratch
Traversy Media
39 Ubuntu Server 14.04 Setup Part 1 - Installation
Ubuntu Server 14.04 Setup Part 1 - Installation
Traversy Media
40 Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Traversy Media
41 Create A Wordpress Widget - Part 1
Create A Wordpress Widget - Part 1
Traversy Media
42 Create A Wordpress Widget - Part 2
Create A Wordpress Widget - Part 2
Traversy Media
43 Create A Wordpress Widget - Part 3
Create A Wordpress Widget - Part 3
Traversy Media
44 Create A Wordpress Widget - Part 4
Create A Wordpress Widget - Part 4
Traversy Media
45 Get Started With Sass on Windows
Get Started With Sass on Windows
Traversy Media
46 Build An HTML5 Template With Bootstrap and SASS - Part 1
Build An HTML5 Template With Bootstrap and SASS - Part 1
Traversy Media
47 Build An HTML5 Template With Bootstrap and SASS - Part 6
Build An HTML5 Template With Bootstrap and SASS - Part 6
Traversy Media
48 Build An HTML5 Template With Bootstrap and SASS - Part 4
Build An HTML5 Template With Bootstrap and SASS - Part 4
Traversy Media
49 Build An HTML5 Template With Bootstrap and SASS - Part 5
Build An HTML5 Template With Bootstrap and SASS - Part 5
Traversy Media
50 Build An HTML5 Template With Bootstrap and SASS - Part 3
Build An HTML5 Template With Bootstrap and SASS - Part 3
Traversy Media
51 Build An HTML5 Template With Bootstrap and SASS - Part 2
Build An HTML5 Template With Bootstrap and SASS - Part 2
Traversy Media
52 Build An HTML5 Template With Bootstrap and SASS - Part 7
Build An HTML5 Template With Bootstrap and SASS - Part 7
Traversy Media
53 Build An HTML5 Template With Bootstrap and SASS - Part 10
Build An HTML5 Template With Bootstrap and SASS - Part 10
Traversy Media
54 Build An HTML5 Template With Bootstrap and SASS - Part 8
Build An HTML5 Template With Bootstrap and SASS - Part 8
Traversy Media
55 Build An HTML5 Template With Bootstrap and SASS - Part 11
Build An HTML5 Template With Bootstrap and SASS - Part 11
Traversy Media
56 Build An HTML5 Template With Bootstrap and SASS - Part 9
Build An HTML5 Template With Bootstrap and SASS - Part 9
Traversy Media
57 Build An Audio Player Using HTML5 & jQuery - Part 1
Build An Audio Player Using HTML5 & jQuery - Part 1
Traversy Media
58 Build An Audio Player Using HTML5 & jQuery - Part 2
Build An Audio Player Using HTML5 & jQuery - Part 2
Traversy Media
59 Youtube Data API v3 & jQuery To List Channel Videos
Youtube Data API v3 & jQuery To List Channel Videos
Traversy Media
60 Using Bootstrap With Ruby on Rails
Using Bootstrap With Ruby on Rails
Traversy Media

This video tutorial teaches how to use Angular 2 template directives to implement conditional statements, switch cases, and loops in templates, which is essential for front-end development in Angular 2.

Key Takeaways
  1. Create a new Angular 2 project or use an existing one
  2. Implement the ngIf directive to conditionally display elements
  3. Use the ngSwitch directive to display different elements based on a condition
  4. Create a loop using the ngFor directive to display a list of items
  5. Apply the directives in a real-world scenario
💡 The ngIf, ngSwitch, and ngFor directives are essential for creating dynamic and interactive templates in Angular 2, and mastering these directives is crucial for front-end development.

Related AI Lessons

I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Learn how a solo dev built a free AI-powered YouTube SEO toolkit with zero budget and the lessons they learned from the experience
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →