<app-root></app-root>
The app-root
is the selector for the app.component
which is the root component of our application.
import { Component } from "@angular/core";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"],
})
export class AppComponent {
title = "my-app";
}
Now take a look at app.component.html
file which represents the view for the app.component
which is the root component of our application.
Start the application by opening the index.html
file in the browser you will be able to see that the app.component
is rendered in the browser.
The app will be running on the port http://localhost:4200/
by default and the output will be :
Now we are linking our newly created component to the app.component.html
file.
You will be see the output like this
import { Component, OnInit } from "@angular/core";
@Component({
selector: "app-app-test",
templateUrl: "./app-test.component.html",
styleUrls: ["./app-test.component.scss"],
})
export class AppTestComponent implements OnInit {
constructor() {}
ngOnInit(): void {}
}
There are three ways to make the changes in the selector :
@Component({
selector: 'app-app-test',
templateUrl: './app-test.component.html',
styleUrls: ['./app-test.component.scss']
})
<app-app-test></app-app-test>
@Component({
selector: '.app-app-test',
templateUrl: './app-test.component.html',
styleUrls: ['./app-test.component.scss']
})
<div class="app-app-test">...</div>
@Component({
selector: '[app-app-test]',
templateUrl: './app-test.component.html',
styleUrls: ['./app-test.component.scss']
})
<div app-app-test>...</div>
Ways to make the changes in the template :
templateUrl
to specify the location of the template file and the template file will be loaded into the component.@Component({
selector: 'app-app-test',
templateUrl: './app-test.component.html',
styleUrls: ['./app-test.component.scss']
})
template
property to specify the template content inside the component.@Component({
selector: 'app-app-test',
template: `<div>Hello World</div>`,
styleUrls: ['./app-test.component.scss']
})
Ways to make the changes in the styles :
styleUrls
property to specify the location of the styles file and the styles file will be loaded into the component.@Component({
selector: 'app-app-test',
templateUrl: './app-test.component.html',
styleUrls: ['./app-test.component.scss']
})
styles
property to specify the styles content inside the component and use it as an inline style.@Component({
selector: 'app-app-test',
templateUrl: './app-test.component.html',
styles: [`
div{
background-color: red;
color: white;
padding: 10px;
border-radius: 5px;
}
`]
})
To know more about Angular Components visit angular.io.
Angular Basics
Previously
Install bootstrap in Angular
Coming up next