Angular Components

In Angular v2, “everything is a component.” Components are the main way in which we build and specify elements and logic on the page, through both custom elements and attributes that add functionality to our existing components.
 
Angular applications are made up of components.

A component is the combination of an HTML template and a component class that controls a portion of the screen. Every component begins with an @Component decorator function that takes a metadata object. The metadata object describes how the HTML template and component class work together. Basically, a component is anything that is visible to the end user and which can be reused many times within an application.

We attach @Component, a TypeScript decorator, which allows us to modify a class or function definition and add metadata to properties and function arguments.
  1. selector -is the element property that we use to tell Angular to create and insert an instance of this component. 
  2. Template -is a form of HTML that tells Angular what needs to be to rendered in the DOM.
  3. MetaData-This is extra data defined for the Angular class.It's defined with a decorator

Now, let's open the app.components.ts page; This is an example of component

  1. import {  
  2.     Component  
  3. } from '@angular/core';  
  4. @Component({  
  5.     selector: 'my-app',  
  6.     template: `<h1>My first {{name}}</h1>`,  
  7. })  
  8. export class AppComponent {  
  9.     name = 'Angular2 Application';  
  10. }  
In the above example, a class decorator is defined in TypeScript. The class normally has the following syntax in TypeScript.
  1. export class AppComponent { name = 'Angular2 Application'; }  
In the above example, the following things need to be noted.

We are defining a class called AppComponent.

  • The export keyword is used so that the component can be used in other modules in the AngularJS application.
  • name is the property.
  • The property is given the type of string.
  • The property is given a value of ‘Angular2 Application’.

    Template defines the user interface, contains the HTML, directives, and data bindings.
Example
  1. template: `<h1>My first {{name}}</h1>`
In this example, the following things need to be noted.
  • We are defining the HTML code which will be rendered in our application.
  • We are also referencing the appTitle property from our class.

Metadata is used to decorate AngularJS class with additional information.

Let’s take a look at the completed code with our class, template, and metadata.

  1. import {  
  2.     Component  
  3. } from '@angular/core';  
  4. @Component({  
  5.     selector: 'my-app',  
  6.     template: `<h1>My first {{name}}</h1>`,  
  7. })  
  8. export class AppComponent {  
  9.     name = 'Angular2 Application';  
  10. }  
In the above example, the following things are needed to be noted.
  • We are using the import keyword to import the ‘Component’ decorator from the angular/core module.
  • We are then using the decorator to define a component.
  • The component has a selector called ‘my-app’. This is nothing but our custom HTML tag which can be used in our main HTML page. 
 Now, let's look into index.html page.
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>Angular QuickStart</title>  
  6.     <base href="/src/">  
  7.     <meta charset="UTF-8">  
  8.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  9.     <link rel="stylesheet" href="styles.css">  
  10.     <!-- Polyfill(s) for older browsers -->  
  11.     <script src="/node_modules/core-js/client/shim.min.js"></script>  
  12.     <script src="/node_modules/zone.js/dist/zone.js"></script>  
  13.     <script src="/node_modules/systemjs/dist/system.src.js"></script>  
  14.     <script src="systemjs.config.js"></script>  
  15.     <script>  
  16.         System.import('main.js').catch(function(err) {  
  17.             console.error(err);  
  18.         });  
  19.     </script>  
  20. </head>  
  21.   
  22. <body>  
  23.     <my-app>Loading AppComponent content here ...</my-app>  
  24. </body>  
  25.   
  26. </html>  
The <body> tag now contains a reference to our custom tag in the component. Thus, in the above case, we need to make sure that the body tag contains the following code.
  1. <body>  
  2.     <my-app>Loading AppComponent content here ...</my-app>  
  3. </body> lt;/html>  
Now, run the application if we got to browser and see the output.

 
Ebook Download
View all
Learn
View all