Angular 2 Data Display
In this chapter, we will introduce how to display data on the user interface. You can use the following three methods:
- Display component properties via interpolation
- Display array properties via NgFor
- Conditional display via NgIf
Displaying Component Properties with Interpolation
To display a component's property, interpolation is the simplest way. The format is:{{propertyName}}。
The following code is based onAngular 2 TypeScript environment configurationto create. You can download the source code in that chapter and modify the files mentioned below.
app/app.component.ts file:
Angular will automatically extract the values of the title and mySite properties from the component and display them in the browser. The displayed information is as follows:

Note:The template is a multi-line string enclosed in backticks (`), not single quotes (').
Using ngFor to Display Array Properties
We can also loop through multiple sites. Modify the following file:
app/app.component.ts file:
In the code, we use Angular's ngFor directive in the template to display each entry in the sites list. Don't forget the leading asterisk (*) in *ngFor.
After modification, the browser displays as follows:

In the example, ngFor loops over an array. In fact, ngFor can iterate over any iterable object.
Next, we create a site.ts file in the app directory. The code is as follows:
app/site.ts file:
The above code defines a class with a constructor and two properties: id and name.
Next, we loop through and output the name property of the Site class:
app/app.component.ts file:
After modification, the browser displays as follows:

Conditional Display with NgIf
We can use NgIf to conditionally output data.
In the following example, we check if the number of websites is more than 3, and output a prompt message: Modify the following app.component.ts file. The code is as follows:
app/app.component.ts file:
After modification, the browser displays as follows, with an additional prompt message at the bottom:
