<div *ngIf="search.length > 1">
</div>
search: string = "";
<div *ngIf="person">
{{person.firstName}}
</div>
Important: There is a huge difference between hiding an element with css and removing it with *ngIf from the DOM. When hidden with css, the element still
uses resources such as memory. *ngIf is most of the time the better approach.
<bal-radio-group [value]="setting" (balChange)="setting=$event.detail"> <bal-radio name="myRadio" value="simple">Simple</bal-radio> <bal-radio name="myRadio" value="advanced">Advanced</bal-radio> </bal-radio-group> <div *ngIf="setting === 'advanced'"> <bal-input placeholder="advanced" [value]="advancedValue" (balInput)="advancedValue=$event.detail!.toString()" ></bal-input> <p *ngIf="advancedValue.length > 2"> Value: {{advancedValue}} </p> </div>
setting: string = "simple"; advancedValue: string = "";
<div *ngIf="search.length > 1">
</div> <template [ngIf]="search.length > 1">
</template>
<li *ngFor="let customer of customers"> {{ customer.name}} </li>
<div *ngFor="let item of items; let idx = index;">
</div>
<div *ngFor="let todo of todos"> <h2>{{todo.title}}</h2> <p>{{todo.desc}}</p> </div>
todos: any[]= [{ title: "*ngFor", desc: "find solution" }, { title: "*ngSwitch", desc: "take a nap" }];
helper boolean variables for odd, even, first and last and the index variable
<div *ngFor="let todo of todos; let idx = index;
let first = first; let last = last;
let even = even; let odd = odd;">
{{idx}} // 0
{{first}} // true
{{last}} // true
{{even}} // true
{{odd}} // false
</div>
Object identity is used to make changes in collections available in the DOM. The behaviour can be controlled with a custom trackBy function. Default is object identity ===
<div *ngFor="let todo of todos; trackBy: myTrackFunc"> </div>
myTrackFunc(index, todo) { return todo.id; }
<div *ngFor="let item of items; let idx = index;">
</div> <template ngFor let--item let-idx="index" [ngForOf]="items"> <div>
</div> </template>
<div [ngSwitch]="selectedFruit">
<apple-view *ngSwitchCase="Fruit.APPLE">
</apple-view>
<orange-view *ngSwitchCase="Fruit.ORANGE">
</orange-view>
<no-fruit-selected *ngSwitchDefault>
</no-fruit-selected>
</div>
for single classes - use a class binding
for multiple classes: use ngClass directive
<div [ngClass]="{
'active': customer.active,
'inactive': !customer.active}">
</div>
for single styles:use a style binding
<span
[style.font-weight]="pers.isVip ? 'bold' : 'normal'">
</span>
for multiple styles: ngStyle directive
<span [ngStyle]="{
'font-size': pers.isVip ? 'bold' : 'normal'
'color': pers.isVip ? 'red' : 'blue'}">
</span>
{{birthday | date:'MM/dd/yy'}}
{{customer | json}}
{{balance | currency:'CHF':false}}
{{title | lowercase | uppercase}}
{{invoice.nr | invoiceNr}}
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({name: 'ellipsis'}) export class EllipsisPipe implements PipeTransform { transform(word: string, count: number): string { if(!count) { return word; } if(word.length <= count) { return word } return word.substring(0, count) + '...'; } }
{{'Lorem ipsum dolor' | ellipsis:10}}
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({name: 'phonenumber'})
export class PhoneNumberPipe implements PipeTransform {
transform(phone: string, countryCode: string): string {
let pts = phone.match(/^(\d{3})(\d{3})(\d{2})(\d{2})$/);
if (!pts) return phone;
let format = `${pts[1]} ${pts[2]} ${pts[3]} ${pts[4]}`;
if (countryCode) {
format = `+${countryCode} ${format.substring(1)}`;
}
return format;
}
}
Name: {{comp?.ceo?.name}}
Name: {{comp && comp.ceo && comp.ceo.name}}