Skip to content

Instantly share code, notes, and snippets.

View LayZeeDK's full-sized avatar
🇩🇰
Denmark

Lars Gyrup Brink Nielsen LayZeeDK

🇩🇰
Denmark
View GitHub Profile
@LayZeeDK
LayZeeDK / modern-singleton.service.ts
Last active December 30, 2018 23:44
Modern singleton service
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class ModernSingletonService {
constructor(
private http: HttpClient,
) {}
@LayZeeDK
LayZeeDK / modern-singleton.service.ts
Last active December 30, 2018 23:49
Modern forRoot alternative for singleton services
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ModernModule } from './modern.module';
@Injectable({
providedIn: ModernModule,
})
export class ModernSingletonService {
constructor(
@LayZeeDK
LayZeeDK / modern-singleton.service.ts
Last active May 5, 2019 18:16
Modern singleton service guarded against multiple injectors
import { HttpClient } from '@angular/common/http';
import { Injectable, Optional, SkipSelf } from '@angular/core';
import { ModernModule } from './modern.module';
@Injectable({
deps: [
[new Optional(), new SkipSelf(), ModernSingletonService],
HttpClient,
],
@LayZeeDK
LayZeeDK / cart-button.component.ts
Last active May 12, 2019 16:55
A SCAM (single component Angular module).
import { Component } from '@angular/core';
@Component({
selector: 'cart-button',
template: `
<button mat-icon-button type="button" (click)="onClick()">
<mat-icon aria-label="Add to shopping cart">shopping_cart</mat-icon>
</button>
`,
})
@LayZeeDK
LayZeeDK / cart-button.component.ts
Last active February 11, 2019 15:15
A tree-shakable component
import { Component } from '@angular/core';
import { MatButton, MatIcon } from '@angular/material';
@Component({
deps: [
MatButton,
MatIcon,
],
selector: 'cart-button',
template: `
@LayZeeDK
LayZeeDK / internet-explorer.module.ts
Last active January 20, 2019 01:29
Angular 4-5 injection token with factory provider
import { NgModule } from '@angular/core';
import { isInternetExplorer11Token } from './is-internet-explorer-11.token';
@NgModule({
providers: [
{
provide: isInternetExplorer11Token,
useFactory: (): boolean =>
/Trident\/7\.0.+rv:11\.0/.test(navigator.userAgent),
@LayZeeDK
LayZeeDK / is-internet-explorer-11.token.ts
Last active January 20, 2019 01:29
Modern injection token with value factory
import { InjectionToken } from '@angular/core';
export const isInternetExplorer11Token: InjectionToken<boolean> =
new InjectionToken('Internet Explorer 11 flag', {
factory: (): boolean =>
/Trident\/7\.0.+rv:11\.0/.test(navigator.userAgent),
providedIn: 'root',
});
@LayZeeDK
LayZeeDK / is-internet-explorer-11.token.ts
Last active January 20, 2019 01:28
Modern injection token with value factory that has dependencies
import { inject, InjectionToken } from '@angular/core';
import { userAgentToken } from './user-agent.token';
export const isInternetExplorer11Token: InjectionToken<boolean> =
new InjectionToken('Internet Explorer 11 flag', {
factory: (): boolean =>
/Trident\/7\.0.+rv:11\.0/.test(inject(userAgentToken)),
providedIn: 'root',
});
@LayZeeDK
LayZeeDK / app.component.ts
Last active May 12, 2019 16:57
Bootstrapping a pre-Ivy Angular application
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'pre-ivy-app',
template: `
<h1>
Hello, {{name}}!
</h1>
`,
})
@LayZeeDK
LayZeeDK / app.component.ts
Last active February 10, 2019 21:16
Bootstrapping an Angular Ivy browser application
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'ivy-app',
template: `
<h1>
Hello, {{name}}!
</h1>
`,
})