Directive
@Directive({
selector: '[autofocus]'
})
export class AutofocusDirective {
private focus = true;
constructor(private el: ElementRef) {
}
ngOnInit() {
if (this.focus) {
window.setTimeout(() => {
this.el.nativeElement.focus();
});
}
}
@Input() set autofocus(condition: boolean) {
this.focus = condition;
}
}
Component
@Component({
selector: 'app-hello',
template: '<div>hello world</div>'
})
export class HelloComponent {
constructor() {
}
}
Custom pipe
@Pipe({ name: 'upper' })
export class UpperPipe implements PipeTransform {
transform(value: any, args: any[] = null): any {
return value ? value.toUpperCase() : '';
}
}
// usage: {{ 'mah' | upper }}
@Input() and @Output
import {
Component,
Input,
Output,
EventEmitter
} from '@angular/core';
@Component({
selector: 'child-component',
template: '<div>child</div>'
}
export class ItemOutputComponent {
@Input() item = ''
@Output() newItemEvent = new EventEmitter<string>();
addNewItem(value: string) {
// newItemEvent event triggers parent callback
this.newItemEvent.emit(value);
}
}
In parent html
<parent-component>
<child-component item="test" (newItemEvent)="callback($event)"></child-component>
</parent-component>
ngFor
<ul>
<li *ngFor="let item of items">{{item}}</li>
</ul>
ngIf: show if valid is set to true
<ul>
<li *ngIf="valid">{{item}}</li>
</ul>
ngClass
<div [ngClass]="'first second'></div>
<div [ngClass]="['first', 'second']></div>
<div [ngClass]="{'first': true, 'second': true, 'third': false}></div>
<div [ngClass]="stringExp|arrayExp|objExp></div>
<div [ngClass]="{'class1 class2 class3' : true}></div>
<div [ngClass]="{ slected: addUserButtonClicked }></div>
<div [ngClass]="[invalidChanges ? 'error-block' : 'info-block']></div>
ngStyle
<some-element [ngStyle]="{'font-style': styleExp}">...</some-element>
<some-element [ngStyle]="{'max-width.px': widthExp}">...</some-element>
<some-element [ngStyle]="objExp">...</some-element>
ngSwitch
<!-- if switch_expression is set to 'match'
then some-element will be displayed -->
<container-element [ngSwitch]="switch_expression">
<!-- the same view can be shown in more than one case -->
<some-element *ngSwitchCase="'match'">...</some-element>
<some-element1 *ngSwitchCase="match_expression_2">...</some-element1>
<some-other-element *ngSwitchCase="match_expression_3">...</some-other-element>
<!--default case when there are no matches -->
<some-element *ngSwitchDefault>...</some-element>
</container-element>
Built-in pipes
DatePipe: Formats a date value according to locale rules.
UpperCasePipe: Transforms text to all upper case.
LowerCasePipe: Transforms text to all lower case.
CurrencyPipe: Transforms a number to a currency string, formatted according to locale rules.
DecimalPipe: Transforms a number into a string with a decimal point, formatted according to locale rules.
PercentPipe: Transforms a number to a percentage string, formatted according to locale rules.
Injectable
a class that needs to be provided and injected as dependency
@Injectable()
export class ApiService {
...
}
ViewChild
Property decorator that configures a view query.
View queries are set before the ngAfterViewInit callback is called.
import {AfterViewInit, Component, Directive, ViewChild} from '@angular/core';
@Directive({selector: 'child-directive'})
class ChildDirective {
}
@Component({selector: 'someCmp', template: 'someCmp.html'})
class SomeCmp implements AfterViewInit {
@ViewChild(ChildDirective) child!: ChildDirective;
ngAfterViewInit() {
// child is set
}
}
ViewChildren used to get arrays of matching views.
ng-content
The element specifies where to project content inside a component template.
<app-parent>
<strong class="app">Developer</strong>
<strong class="app1">app 1 content</strong>
</app-parent>
Only select elements from the projected content that match the given CSS selector.
<div>
<span>Hello, I am </span>
<ng-content select=".app"></ng-content>
</div>
<div>
<ng-content select=".app1"></ng-content>
</div>
ContentChild
Property decorator that configures a content query.
Content queries are set before the ngAfterContentInit callback is called.
import {AfterContentInit, Component, Directive, ViewChild} from '@angular/core';
@Directive({selector: 'child-directive'})
class ChildDirective {
}
@Component({
selector: 'someCmp',
template: '<div><ng-content></ng-content></div>'
})
class SomeCmp implements AfterContentInit {
@ContentChild(ChildDirective) child!: ChildDirective;
ngAfterContentInit() {
// Content is set
}
}
ContentChildren used to get arrays of matching Content.
ng-template & ng-container
ng-template defines a template that is not rendered by default.
<ng-template #ref>html content</ng-template>
ng-container element that can hold structural directives without adding new elements to the DOM.
<ng-container *ngIf="true">
<!-- here ng-container will not be added to dom -->
<div>test</div>
</ng-container>