AngularJS2 data
This chapter we will tell you how the data is displayed on the user interface, you can use the following three ways:
- Display properties of a component by interpolation expression
- By NgFor display an array type attribute
- By NgIf achieve conditional display
Display properties of a component by interpolation expression
To display the component properties, interpolation is the easiest way to format:{{name}} attribute.
The following code is based AngularJS2 TypeScript environment configuration to create, and then you can download the source code on that section, and modify a few files mentioned below.
app / app.component.ts file:
Angular automatically extract value from the property title and mySite assembly, and displayed in the browser, the following information is displayed:
Note: The template is a packet string backquote ( `) in a multi-line, rather than a single quote ( ').
Use ngFor display array property
We can also loop output multiple sites, modify the following files:
app / app.component.ts file:
Code to display the sites we list each entry in the template Angular ngFor instruction, do not forget * ngFor the leading asterisk (*). .
After modification, the browser displays the following:
Example ngFor circulating an array fact ngFor can iterate over any object that can be iterative.
Next we create site.ts files in app directory, as follows:
app / site.ts file:
The above code defines a constructor and two properties with: id and name of the class.
Then we loop output Site name attribute class:
app / app.component.ts file:
After modification, the browser displays the following:
Conditional display by NgIf
We can use the data to set the output NgIf specified conditions.
The following examples, we determine if the site having 3 or more, the output message: Modify the following app.component.ts file, as follows:
app / app.component.ts file:
After modification, the browser displays as shown below, at the bottom of more than a message: