Description
You can display the data with the help of binding controls in the UI. Angular will display the data by using interpolation and other binding properties such as using binding in HTML template to the Angular component properties.Example
The below example describes use of displaying data in the Angular 2:<!DOCTYPE html> <html> <head> <title>Angular 2 Data Display</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.20/system-polyfills.js"></script> <script src="https://code.angularjs.org/2.0.0-beta.6/angular2-polyfills.js"></script> <script src="https://code.angularjs.org/tools/system.js"></script> <script src="https://code.angularjs.org/tools/typescript.js"></script> <script src="https://code.angularjs.org/2.0.0-beta.6/Rx.js"></script> <script src="https://code.angularjs.org/2.0.0-beta.6/angular2.dev.js"></script> <script> System.config({ transpiler: 'typescript', typescriptOptions: { emitDecoratorMetadata: true }, packages: {'app': {defaultExtension: 'ts'}}, map: { 'app': './angular2/src/app' } }); System.import('app/datadisplay_main') .then(null, console.error.bind(console)); </script> </head> <body> <my-app>Loading...</my-app> </body> </html>The above code includes the following configuration options:
-
You can configure the index.html file using typescript version. The SystemJS transpile the TypeScript to JavaScript before running the application by using the transpiler option.
-
If you do not transpile to JavaScript before running the
application, you could see the compiler warnings and errors which are
hidden in the browser.
-
The TypeScript generates metadata for each and every class of the code when the emitDecoratorMetadata
option is set. If you don't specify this option, large amount of unused
metadata will be generated which affects the file size and impact on
the application runtime.
-
Angular 2 includes the packages form the app folder where files will have the .ts extension.
-
Next it will load the main component file from the app folder. If there is no main component file found, then it will display the error in the console.
-
When Angular calls the bootstrap function in main.ts, it reads
the Component metadata, finds the 'app' selector, locates an element tag
named app, and loads the application between those tags.
datadisplay_main.ts
import {bootstrap} from "angular2/platform/browser" import {MyTemplate} from "./datadisplay_app.component" bootstrap(MyTemplate);Now we will create a component in TypeScript(.ts) file as shown below:
datadisplay_app.component.ts
import {Component, View} from "angular2/core"; @Component({ selector: 'my-app' }) @View({ template: ` <h2>Showing data using component properties with interpolation</h2> <h3>Player Name:{{player}}</h3> <h3>He is famous in: {{sport}}</h3><br> <h2>Showing data using constructor or variable initialization</h2> <h3>India capital is: {{capital}}</h3><br> <h2>Showing data using array property with NgFor</h2> <h3>My favorite fruit is: {{myfruit}}</h3> <p>List of Fruits:</p> <ul> <li *ngFor="#fruit of fruits"> {{ fruit }} </li> </ul> ` }) export class MyTemplate { player: 'M.S. Dhoni '; sport:'Cricket'; capital: string; constructor() { this.capital = 'New Delhi'; } fruits = ['Apple', 'Orange', 'Mango', 'Grapes']; myfruit = this.fruits[1]; }
- The @Component is a decorator which uses configuration object to create the component.
- The selector creates an instance of the component where it finds <my-app> tag in parent HTML.
- The @view contains a template that tells Angular how to render a view.
- The export specifies that the component will be available outside the file.
Output
Let's carry out the following steps to see how above code works:- Save above HTML code as index.html file as how we created in environment chapter and use the above app folder which contains .ts files.
- Open the terminal window and enter the below command:
npm start
- After few moments, a browser tab should open and displays the output as shown below.
- Save above HTML code as angular2_data_display.html file in your server root folder.
- Open this HTML file as http://localhost/angular2_data_display.html and output as below gets displayed.
No comments:
Post a Comment