Angular: Sudo NPM Install - G @angular/cli
Angular: Sudo NPM Install - G @angular/cli
Angular Fundamentals
Module
o Collection of classes/enums/functions
o Angular uses NgModule to create a module
o Every application must contain at least one module
(AppModule)
o E.g.
@NgModule({
declarations: [],
imports: [],
providers: [],
bootstrap: []
})
export class AppModule {
}
o where:
declarations:
contains the list of classes which part of this
module
it may contain
o Component class
o Pipe class
o Directive class
imports:
Contains list of dependency module
providers:
contains list of classes which provide services
contains list of services (classes)
bootstrap:
list of classes which will be loaded by default
Component
o Re-usable entity
o Encapsulates
Data
To render the data, class uses data members
View:
Uses the template and CSS to render a view
Logic
o Angular uses @Component to create a new component
o E.g.
import {Component} from ‘@angular/core’;
@Component({
selector: ‘<tag name>’,
template: ‘<html code>’
})
export class MyComponent {
}
o Where
selector: tag which is used as a container
template: used to define the html code (view)
styles:
templateUrls:
styleUrls:
o To add a custom component:
Create a component class inside src/app
(my.component.ts)
Add meta-data related to Component
@Component({
selector: 'my-component',
template: '<h1>My Component</h1>'
})
export class MyComponent {
Metadata
o Extra information added to the class
Binding
Types
o Interpolation
Use {{ }} to insert the value of a variable
E.g.
o Attribute binding
o Class Binding
o Event Binding
toggle() {
// occur an event
this.favoriteToggled.emit();
}
}
Directive
Instruction(s) given to angular to perform some action(s)
Types
o *ngFor
used to iterate over a collection
exposes built-in variables
index: position
first: true if the element is at the first position
last: true if the element is at the last position
even: true if the position is even position
odd: true if the position is odd position
syntax:
<div *ngFor=”let <tmp> of <collection>”></div>
E.g.
<div *ngFor=”let contact of contacts”></div>
o *ngIf
used to add a conditional logic
Syntax:
<div *ngIf=”<condition>”></div>
E.g.
<div *ngIf=”contacts.length == 0”>no
contacts</div>
the div will be added to the page when the
contacts array is empty
otherwise the div element will NOT be added
o *ngSwitchCase
used to select an element based on condition
Syntax:
<div [ngSwitch]=”<variable>”>
<div *ngSwitchCase=”<value>”>
contents</div>
</div>
E.g.
selectedValue = 1;
<div [ngSwitch]=”selectedValue”>
<div *ngSwitchCase=”1”> value 1</div>
<div *ngSwitchCase=”2”> > value 2</div>
<div *ngSwitchCase=”3”> > value 3</div>
</div>
Imports: [
BrowserModule,
HttpModule
]