Skip to content

Instantly share code, notes, and snippets.

@SaurabhLpRocks
Created May 10, 2018 09:06
Show Gist options
  • Select an option

  • Save SaurabhLpRocks/003ed1a3f911e7e50d6cc23a34459e8a to your computer and use it in GitHub Desktop.

Select an option

Save SaurabhLpRocks/003ed1a3f911e7e50d6cc23a34459e8a to your computer and use it in GitHub Desktop.
Angular Performance tips
import {AfterViewInit, ChangeDetectorRef} from '@angular/core';
@Component(…)
class AppComponent implements AfterViewInit {
constructor(private cdr: ChangeDetectorRef) {}
ngAfterViewInit() {
// We only want to detach the change detectors after change detection has been
// performed for the first time
this.cdr.detach();
}
update() {
// Run change detection only for this component when update() method is called.
this.cdr.detectChanges();
}
}
import {enableProdMode} from '@angular/core';
if (ENV === 'production') {
enableProdMode();
}
var compression = require('compression')
var express = require('express')
var app = express()
app.use(compression())
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {share} from 'rxjs/operators';
@Injectable()
export class AppService {
data: Observable < any >;
constructor(private http : HttpClient) {
this.data = this.http.get<any> ('apiUrl').pipe(share());
}
getData() {
return this.data;
}
}
@Component({
selector: 'app',
template: `<ul>
  <li *ngFor="let item of items; trackBy: trackById">{{item.name}}</li>
</ul>`
})
class AppComponent {
Items = [
{
id: 1,
name: 'item 1'
}, {
id: 2,
name: 'item 2'
},
...
];
trackById(index, item) {
return item.id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment