Common Syntax
Template Syntax
Attribute Binding
Sometimes there are differences between the name of property and an attribute.
- colspan is an attribute of
<td>, while colSpan with a capital "S" is a property.- When using attribute binding, use colspan with a lowercase "s".
Class and Style Binding
The expression can be one of:
- A space-delimited string of class names.
- An object with class names as the keys and truthy or falsy expressions as the values.
- An array of class names.
@Component({
selector: 'app-nav-bar',
template: `
<nav [style]='navStyle'>
<a [style.text-decoration]="activeLinkStyle">Home Page</a>
<a [style.text-decoration]="linkStyle">Login</a>
</nav>`
})
export class NavBarComponent {
navStyle = 'font-size: 1.2rem; color: cornflowerblue;';
linkStyle = 'underline';
activeLinkStyle = 'overline';
/* . . . */
}
Conditionals
These are Angular structural directives used for conditional rendering.
ngIf
Use
ngIffor conditional rendering or apply CSS classes or style to adjust the display of certain elements. There is no AngularJS equivalent ofng-showorng-hide.
ngSwitch, ngSwitchCase
Containers and Templates
ngContainer
The <ng-container> allows us to use structural directives without any extra element, making sure that the only DOM changes being applied are those dictated by the directives themselves.
Multiple Structural Containers
https://angular.io/api/core/ng-container#combination-of-multiple-structural-directives
Event Binding
Plurals
Use the ngPlural directive to render plural variations for an element.
Property Binding
Repeaters
ngFor, ngForOf
ngFor and ngForOf directives are used together to render a group of items, such as a list.
The following exported values can be aliased to local variables:
$implicit: T: The value of the individual items in the iterable (ngForOf).ngForOf: NgIterable<T>: The value of the iterable expression. Useful when the expression is more complex then a property access, for example when using the async pipe (userStreams | async).index: number: The index of the current item in the iterable.count: number: The length of the iterable.first: boolean: True when the item is the first item in the iterable.last: boolean: True when the item is the last item in the iterable.even: boolean: True when the item has an even index in the iterable.odd: boolean: True when the item has an odd index in the iterable.
Lifecycle Hooks
https://angular.io/guide/glossary#lifecycle-hook
| HOOK METHOD | DETAILS |
|---|---|
| ngOnChanges | When an input or output binding value changes. |
| ngOnInit | After the first ngOnChanges. |
| ngDoCheck | Developer's custom change detection. |
| ngAfterContentInit | After component content initialized. |
| ngAfterContentChecked | After every check of component content. Called once after ngAfterContentInit and every time after ngDoCheck. |
| ngAfterViewInit | After the views of a component are initialized. |
| ngAfterViewChecked | After every check of the views of a component. Called once after ngAfterViewInit and every time after ngDoCheck. |
| ngOnDestroy | Just before the directive is destroyed. |
OnInit
A lifecycle hook that is called after Angular has initialized all data-bound properties of a directive. Define an ngOnInit() method to handle any additional initialization tasks.
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnInit {
ngOnInit() {
// ...
}
}
OnDestroy
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnDestroy {
ngOnDestroy() {
// ...
}
}
OnChanges
A callback method that is invoked immediately after the default change detector has checked data-bound properties if at least one has changed, and before the view and content children are checked.
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnChanges {
@Input() prop: number = 0;
ngOnChanges(changes: SimpleChanges) {
// changes.prop contains the old and the new value...
}
}
Sample File Shells
Component
https://angular.io/guide/component-overview
A component can be defined in a single
.tsfile or split up into.ts,.scss, and.htmlfiles.
Single file
- src/app/components/example.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
//import { ExampleService } from '../services/example.service';
@Component({
selector: 'app-example',
template: `
<h1>Hello World!</h1>
<p>This template definition spans multiple lines.</p>
`,
styles: ['h1 { font-weight: normal; }']
})
export class ExampleComponent implements OnInit, OnDestroy {
constructor (/*private exampleService: ExampleService*/) { }
ngOnInit() {}
ngOnDestroy() {}
}
Multiple files
- src/app/components/example.component.html
- src/app/components/example.component.scss
- src/app/components/example.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
//import { ExampleService } from '../services/example.service';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: [ './example.component.scss' ]
})
export class ExampleComponent implements OnInit, OnDestroy {
constructor (/*private exampleService: ExampleService*/) { }
ngOnInit() {}
ngOnDestroy() {}
}
Directive
Attribute Directive
import { Directive } from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
}
Apply the directive...
Service
https://angular.io/guide/creating-injectable-service
src/app/services/example.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class ExampleService {
getSomething() { return ""; }
}