Runoob Tutorial ArchiveEnglish

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

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

<

h2

My 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:

BAD 55 1 31 4 4 6C 8A6B 5AF 8613557A

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

<

h2

My favorite websites:

{

{

mySite

}

}

</

h2

<

p

Website list:

p

<ul>
<li ngFor=">

let site of sites

"

{

{

site

}

}

</

li

</

ul

`

}

)

export

class

AppComponent

{

title

=

'

Site list

'

;

sites

=

[

'

Beginner Tutorial

'

,

'

Google

'

,

'

Taobao

'

,

'

Facebook

'

]

;

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:

588F 11 7BAC 4 A3 B8A9 6A8DD 97BBF

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

<

h2

My favorite websites:

{

{

mySite

.

name

}

}

</

h2

<

p

Website 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

,

'

Google

'

)

,

new

Site

(

3

,

'

Taobao

'

)

,

new

Site

(

4

,

'

Facebook

'

)

]

;

mySite

=

this

.

sites

[

0

]

;

}

After modification, the browser displays as follows:

588F 11 7BAC 4 A3 B8A9 6A8DD 97BBF


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

<

h2

My favorite websites:

{

{

mySite

.

name

}

}

</

h2

<

p

Website 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

,

'

Google

'

)

,

new

Site

(

3

,

'

Taobao

'

)

,

new

Site

(

4

,

'

Facebook

'

)

]

;

mySite

=

this

.

sites

[

0

]

;

}

After modification, the browser displays as shown below, with an additional prompt at the bottom:

7931418A C914 46D3 9EF6 85CAF666FA9B