Skip to content

Instantly share code, notes, and snippets.

View Oleg-Sulzhenko's full-sized avatar

Oleh Oleg-Sulzhenko

View GitHub Profile
There are three ways to do this:
1) Put an *ngIf in parent. Only render child when parent's items is ready.
<div *ngIf="items">
<child [items]="items | async">
</div>
2) Separate your input getter setter in child. Then act whenever the value is set, you can use RxJS BehaviorSubject also.
private _items = new BehaviorSubject<Items[]>([]);
@Input() set items(value: Items[]) {
public openModal(jobToEdit: RecurrentJob): void {
let dialogRef = this.dialog.open(AddEditJobFormComponent, {
width: '600px',
data: { exampleDataPassedToDialog: 'some Data' } // you can pass Data here instead of Input()
});
dialogRef.componentInstance.jobToEdit = jobToEdit;
dialogRef.componentInstance.formData.subscribe((formData: RecurrentJob) => {
this.newJob.next(formData);
});
dialogRef.afterClosed().pipe(take(1)).subscribe(() => { dialogRef = null; });
1) trackBy
When an array changes, Angular re-renders the whole DOM tree. But if you use trackBy,
Angular will know which element has changed and will only make DOM changes for that particular element.
<li *ngFor="let item of items; trackBy: trackByFn">{{ item }}</li>
trackByFn(index, item) {
return item.id; // unique id corresponding to the item
}
switchMap: when you want to ignore the previous emissions when there is a new emission
mergeMap: when you want to concurrently handle all the emissions
concatMap: when you want to handle the emissions one after the other as they are emitted
exhaustMap: when you want to cancel all the new emissions while processing a previous emisssion
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
constructor(
private fb: FormBuilder
) {
this.complexForm = fb.group({
firstName : [null, Validators.required],
lastName: [null, Validators.compose([Validators.required, Validators.minLength(5), Validators.maxLength(10)])],
gender: ['Female'],
hiking : false,
function isValidIP(ip_str) {if(/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ip_str)) {return true;} else return false;}