Angular 2 Data Display
Angular 2 Data Display
In this chapter, we will introduce how to display data on the user interface using the following three methods:
- Displaying component properties using interpolation expressions
- Displaying array properties using NgFor
- Implementing conditional display using NgIf
Displaying component properties using interpolation expressions
Interpolation is the simplest way to display component properties, in the format:{{property name}}.
The following code is based on Angular 2 TypeScript Environment Configuration. You can download the source code in this chapter and modify the following files mentioned.
app/app.component.ts file:
import
{
Component
}
from
'
@angular/core
'
; @
Component
(
{
selector
:
'
my-app
'
,
template
: <
h1
{
{
title
}
}
</
h1
<
h2My favorite websites:
{
{
mySite
}
}
</
h2
`
}
)
export
class
AppComponent
{
title
=
'Site List
'
;
mySite
=
'
Beginner Tutorial
'
;
}
Angular will automatically extract the values of the title and mySite properties from the component and display them in the browser, showing the following information:

Note:The template is a multi-line string enclosed in backticks (`), not single quotes (').
Use ngFor to display array properties
We can also loop through multiple sites by modifying the following file:
app/app.component.ts file:
import
{
Component
}
from
'
@angular/core
'
; @
Component
(
{
selector
:
'
my-app
'
,
template
: <
h1
{
{
title
}
}
</
h1
<
h2My favorite websites:
{
{
mySite
}
}
</
h2
<
pWebsite list:
p
<ul> <li ngFor=">let site of sites
"
{
{
site
}
}
</ li </ ul ` } ) export class AppComponent { title = 'Site list
'
;
sites
=
[
'
Beginner Tutorial
'
,
'
'
,
'
Taobao
'
,
'
'
]
;
mySite
=
this
.
sites
[
0
]
;
}
In the code, we use the Angular ngFor directive in the template to display each item in the sites list; don't forgetthe leading asterisk (*) in ngFor.After modification, the browser displays as follows:
After modification, the browser displays as shown below:
In the example, ngFor loops over an array, but in fact, ngFor can iterate over any iterable object.
Next, we create the site.ts file in the app directory, the code is as follows:
app/site.ts file:
export
class
Site
{
constructor
(
public
id
:
number
,
public
name
:
string
)
{
}
}
The code above defines a class with a constructor and two properties: id and name.
Next, we loop to output the name property of the Site class:
app/app.component.ts file:
import
{
Component
}
from
'
@angular/core
'
;
import
{
Site
}
from
'
./site
'
; @
Component
(
{
selector
:
'
my-app
'
,
template
: ` <
h1
{
{
title
}
}
</ h1 < h2My favorite websites:
{
{
mySite
.
name
}
}
</
h2
<
pWebsite list:
p
<ul> <li ngFor=">let site of sites
"
{
{
site
.
name
}
}
</ li </ ul ` } ) export class AppComponent { title = 'Site list
'
;
sites
=
[
new
Site
(
1
,
'
Beginner Tutorial
'
)
,
new
Site
(
2
,
'
'
)
,
new
Site
(
3
,
'
Taobao
'
)
,
new
Site
(
4
,
'
'
)
]
;
mySite
=
this
.
sites
[
0
]
;
}
After modification, the browser displays as follows:
Conditional display using NgIf
We can use NgIf to set data for conditional output.
In the following example, we check if the number of websites is 3 or more and output a prompt message: Modify the following app.component.ts file, the code is as follows:
app/app.component.ts file:
import
{
Component
}
from
'
@angular/core
'
;
import
{
Site
}
from
'
./site
'
; @
Component
(
{
selector
:
'
my-app
'
,
template
: ` <
h1
{
{
title
}
}
</ h1 < h2My favorite websites:
{
{
mySite
.
name
}
}
</
h2
<
pWebsite list:
p
<ul> <li ngFor=">let site of sites
"
{
{
site
.
name
}
}
</ li </ ul < p ngIf = "sites.length > 3
"
You have many favorite websites!
p
`
}
)
export
class
AppComponent
{
title
=
'
Site List
'
;
sites
=
[
new
Site
(
1
,
'
Beginner Tutorial
'
)
,
new
Site
(
2
,
'
'
)
,
new
Site
(
3
,
'
Taobao
'
)
,
new
Site
(
4
,
'
'
)
]
;
mySite
=
this
.
sites
[
0
]
;
}
After modification, the browser displays as shown below, with an additional prompt at the bottom:

