Angular - Cheat Sheet
Angular - Cheat Sheet
io
@NgModule({
declarations: ...,
imports: ..., Defines a module that contains
exports: ..., components, directives, pipes, and
providers: ..., providers.
bootstrap: ...})
class MyModule {}
declarations:
[MyRedComponent, List of components, directives, and
MyBlueComponent, pipes that belong to this module.
MyDatePipe]
List of modules to import into this
imports:
module. Everything from the
[BrowserModule,
imported modules is available to
SomeOtherModule] declarations of this module.
Template syntax
Binds property value to the result of
<input [value]="firstName">
expression firstName.
Binds attribute role to the result of
<div [attr.role]="myAriaRole">
expression myAriaRole.
<svg>
An <svg> root element is detected as
<rect x="0" y="0" width="100"
an SVG element automatically,
height="100"/>
without the prefix.
</svg>
import {
Built-in directives CommonModule } from
'@angular/common';
Removes or recreates a
portion of the DOM tree
<section *ngIf="showSection"> based on the
showSection
expression.
<div
[ngSwitch]="conditionExpression">
<ng-template Conditionally swaps the
[ngSwitchCase]="case1Exp">...</ng- contents of the div by
template> selecting one of the
<ng-template embedded templates
ngSwitchCase="case2LiteralString">... based on the current
</ng-template> value of
conditionExpression.
<ng-template ngSwitchDefault>...
</ng-template>
</div>
Binds the presence of
CSS classes on the
element to the truthiness
<div [ngClass]="{'active': isActive, of the associated map
'disabled': isDisabled}"> values. The right-hand
expression should return
{class-name: true/false}
map.
import { FormsModule }
Forms
from '@angular/forms';
@Component({...})
Declares that a class is a component and
class
provides metadata about the component.
MyComponent() {}
@Directive({...})
Declares that a class is a directive and
class
provides metadata about the directive.
MyDirective() {}
@Directive({ property1:
Directive configuration
value1, ... })
Specifies a CSS selector that identifies
this directive within a template.
Supported selectors include element,
selector: '.cool-
[attribute], .class, and :not().
button:not(a)'
Does not support parent-child
relationship selectors.
providers:
List of dependency injection providers for
[MyService, {
this directive and its children.
provide: ... }]
@Component extends
@Directive, so the @Directive
Component configuration
configuration applies to
components as well
template: 'Hello
{{name}}' Inline template or external template
templateUrl: 'my- URL of the component's view.
component.html'
styles: ['.primary
List of inline CSS styles or external
{color: red}']
stylesheet URLs for styling the
styleUrls: ['my-
component’s view.
component.css']
Called after
ngAfterContentInit
ngAfterViewInit() { ... } when the component's view
has been initialized. Applies
to components only.
Dependency injection
configuration
import { Routes,
RouterModule, ...
Routing and navigation
} from
'@angular/router';
Creates a link to a
children: ... }