view
, which is the area of the screen that a user seesA real world application might looks like this:
Every application has at least one component which we call App Component
or Root Component
. A real world Angular app is essentially a tree of Components:
App Module
hello-world
projectng serve --open
src\app
folder and create file named "courses.component.ts"Decorator
called Component from Angular core libraryimport { Component } from '@angular/core';
Decorator function
Component and pass in an object to tell Angular how this component worksselector
: css-style element e.g. courses, .courses, #coursestemplate
: HTML markup we want to render for this component. If the template is an external file, we use templateUrl
instead @Component({
selector: 'courses',
template: '<h2>Courses</h2>'
})
template
for this component inside that selector
element@NgModule
is used to convert a TypeScript class to a ModuleAppComponent
which is part of the app module
declarations
which is where we add all the Components that are part of app module
declarations
, VS code automatically insert the "import ..." at the topapp.component.html
is an external template for our "AppComponent"localhost:4200
// File: app.component.html
<h1>Angular</h1>
<courses></courses>
Inspect Element
src/index.html
app/app.component.ts
app.component.html
is external[name].component.ts
[Name]Component
Component
in the name of the classng g c course
where "g" is short for "generate" and "c" is short for "component"
Angular-CLI created a folder called "course" and add 4 files inside the foler: a CSS file, a HTML file, a unit test file and a TypeScript file. It also registered the component in AppModule
.
Interpolation
syntax {{title}}
to render title dynamicallyData Binding
in which you bind a view to a field in this componentimport { Component } from '@angular/core';
@Component({
selector: 'courses',
template: '<h2>{{ getTitle() }}</h2>'
})
export class CoursesComponent {
title = "List of courses";
getTitle(){
return this.title;
}
}
back tick
so that the list can be spanned multiple lines*ngFor
with string interpolation
to manipulate the DOMimport { Component } from '@angular/core';
@Component({
selector: 'courses',
template: `
<h2>{{ title }}</h2>
<ul>
<li *ngFor="let course of courses">
{{ course }}
</li>
</ul>
`
})
export class CoursesComponent {
title = "List of courses";
courses = ["course1", "course2", "course3"];
}
We implement above by:
Add logic for calling an HTTP service
Issues:
app\courses.service.ts
// File courses.services.ts
export class CoursesService{
getCourses(){
return ["course1", "course2", "course3"];
}
}
constructor
to class CoursesComponentnew
, note that import statement is automatically added to the top of courses component// File: courses.component.ts (partial)
export class CoursesComponent {
title = "List of courses";
courses;
constructor(){
let service = new CoursesService();
this.courses = service.getCourses();
}
}
However, there are problems with this implementation:
new
operator, we have tightly coupled
the class ("CoursesComponent") with its implementation ("CoursesService")decoupled
the class ("CoursesComponent") from its dependency ("CoursesService")// File: courses.component.ts (partial)
export class CoursesComponent {
title = "List of courses";
courses;
constructor(service: CoursesService){
this.courses = service.getCourses();
}
}
Step2: Register "CoursesService" as a provider in app module
Dependency Injection
)Angular has a dependency injection framework built in to it. In order for that to work, you need to register the dependency ("CoursesService") in as a provider
in AppModule
:
If you miss this step, the console will give you an error - Error: No provider for CoursesService
ng g s email
@injectable
is a decorator function which is required ONLY if this service had denpendency in its constructorinjectible class
which means Angular should be able to inject dependency of this class into its constructor@injectable
decorator when defining components because when we use the @component
decorator, that decorator internally includes these @injectable
decorator