Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Creating Our First Component in Angular 2

DZone's Guide to

Creating Our First Component in Angular 2

In this article, you'll build a simple hello world application in Angular using a few different components to create the basic logic and HTML structure.

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Hi friends!

In our previous article, we learned that Angular applications are made of several components, each representing its own View. Refer to my previous article for better understanding.

Image title

And, we learned about app.component, which is a responsible component for displaying the content in our browser. Now, in this article, we shall see how to create our own component in Angular 2 applications.

The first thing we have to do is to create a new TypeScript file.

Image title

I have added a file called rathrola.component.ts. Just like our app.component, we have imported the component class from core module, as shown below.

import {
    Component
} from '@angular/core';
@Component({
    selector: "my-tuts",
    template: `<h2>Rathrola Prem Kumar, Consultant</h2>`,
})
export class RathrolaComponent {}

If you remember, a component is nothing but a class with metadata. Let’s name our class rathrolaComponentand, since we need to use this class in other components, we are going to export it.

And now, we shall attach the component decorator of our class. Within this component decorator, we are going to specify a selector and a template as shown below.

@Component({
    selector: "my-tuts",
    template: `<h2>Rathrola Prem Kumar, Consultant</h2>`,
})

In the template, we created a simple HTML tag, as shown above.

Now, we have an import statement, component decorator, and associated class. Finally, our newly created component is complete.

In my previous article, I mentioned that, usually, in Angular applications, we will have one main component called a root component and there are other components that are part of this root component.

In our case, the root component is app.component and rathrola.component is going to live inside app.component.

So now, from app.component, remove the single line quotes in the template in the component decorator, as shown below:

import {
    Component
} from '@angular/core';
@Component({
    selector: 'my-app',
    template: `<h1>Hello World From Rathrola Prem Kumar</h1>`,
})
export class AppComponent {}

So, remove the single quotes and keep the backtick (`). This will be below the esc key on the keyboard.

This is going to allow us to specify our HTML in multiple lines. Now, what is the HTML we need to specify?

It is selector of rathrola.componentas shown below:

import {
    Component
} from '@angular/core';
import {
    RathrolaComponent
} from './rathrola.component'
@Component({
    selector: 'my-app',
    template: `<h1>Hello World From Rathrola Prem Kumar</h1>
<h4>Header 4 from App component</h4>
<my-tuts></my-tuts>`,
    styles: [`h4{
color:blue;
}`],
    directives: [RathrolaComponent]
})
export class AppComponent {} // This is just a sample script. Paste your real code (javascript or HTML) here.
if ('this_is' == /an_example/) {
    of_beautifier();
} else {
    var a = b ? (c % d) : e[f];
}

directives is going to have an array - it is just a name of the component, as shown below:

directives: [RathrolaComponent] 

Note: we need to import rathrola.component before we can create the directive as shown above.

Run the application and you should get an output similar to what I have below.

Image title

This is how we create a new component and then use it in other component using directives.

In our next article, we shall see how to style the HTML code for a component.

Thanks for reading my article, and stay tuned!

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
angular 2 ,web dev ,angular ,web application development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}