Angular Main
Angular Main
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
app.component.css
app.component.html
app.component.spec.ts
app.component.ts
app.module.ts
The above files were created by default when we created new project using
the angular-cli command.
If you open up the app.module.ts file, it has some libraries which are
imported and also a declarative which is assigned the appcomponent as
follows −
Now, if we go and check the file structure, we will get the new-cmp new
folder created under the src/app folder.
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent] //for bootstrap the AppComponent the main app
component is given.
})
@Component({
// this is a declarator which starts with @ sign. The component word marked in
bold needs to be the same.
selector: 'app-new-cmp', //
templateUrl: './new-cmp.component.html',
})
constructor() { }
ngOnInit() {}
Let us check how the flow works. Now, the app component, which is
created by default becomes the parent component. Any component added
later becomes the child component.
<head>
<title>Angular 4App</title>
</head>
<body>
<app-root></app-root>
</body>
</html>
The above is the normal html file and we do not see anything that is printed
in the browser. Take a look at the tag in the body section.
<app-root></app-root>
This is the root tag created by the Angular by default. This tag has the
reference in the main.ts file.
if (environment.production) {
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
AppModule is imported from the app of the main parent module, and the
same is given to the bootstrap Module, which makes the appmodule load.
@NgModule({
declarations: [
AppComponent,
NewCmpComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
Here, the AppComponent is the name given, i.e., the variable to store the
reference of the app. Component.ts and the same is given to the
bootstrap. Let us now see the app.component.ts file.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
Angular core is imported and referred as the Component and the same is
used in the Declarator as −
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
It has just the html code and the variable title in curly brackets. It gets
replaced with the value, which is present in the app.component.ts file.
This is called binding. We will discuss the concept of binding in a
subsequent chapter.
Now that we have created a new component called new-cmp. The same
gets included in the app.module.ts file, when the command is run for
creating a new component.
new-cmp.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-new-cmp',
templateUrl: './new-cmp.component.html',
styleUrls: ['./new-cmp.component.css']
})
Here, we have to import the core too. The reference of the component is
used in the declarator.
As seen above, we have the html code, i.e., the p tag. The style file is
empty as we do not need any styling at present. But when we run the
project, we do not see anything related to the new component getting
displayed in the browser. Let us now add something and the same can be
seen in the browser later.
<app-new-cmp></app-new-cmp>
Let us see the new component .html file and the new-
cmp.component.tsfile.
new-cmp.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-new-cmp',
templateUrl: './new-cmp.component.html',
styleUrls: ['./new-cmp.component.css']
})
export class NewCmpComponent implements OnInit {
newcomponent = "Entered in new component created";
constructor() {}
ngOnInit() { }
}
In the class, we have added one variable called new component and the
value is “Entered in new component created”.
<p>
new-cmp works!
</p>
Similarly, we can create components and link the same using the selector
in the app.component.html file as per our requirements.