Skip to content

Instantly share code, notes, and snippets.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { ModuleAModule } from './moduleA/moduleA.module';
@NgModule({
declarations: [
import { ModuleWithProviders, NgModule } from '@angular/core';
import { SingletonService } from './singleton.service';
import { ModuleAComponent } from './moduleA.component';
@NgModule({
declarations: [
ModuleAComponent
],
exports: [ModuleAComponent],
const moduleWithProviders = {
ngModule: ModuleAModule,
providers: [SingletonService]
};
@NgModule({
declarations: [
AppComponent
],
import { Component, OnInit } from '@angular/core';
import { SingletonService } from './singleton.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(
@Dornhoth
Dornhoth / singleton.service.ts
Created September 11, 2020 19:15
Singleton
import { Injectable } from '@angular/core';
@Injectable()
export class SingletonService {
static instances = 0;
constructor() {
SingletonService.instances += 1;
}
@Dornhoth
Dornhoth / app.module.ts
Created September 11, 2020 18:25
Providers Array
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
@Dornhoth
Dornhoth / test.service.ts
Created September 11, 2020 16:06
Provide In root
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class TestService {
getTest(): string {
return 'test';
}
}
@Dornhoth
Dornhoth / test.component.ts
Last active September 11, 2020 15:37
Service Injection
import { Component, OnInit } from '@angular/core';
import { TestService } from 'src/app/services/test.service';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.scss'],
})
export class TestComponent implements OnInit {
text: string;
import {
Directive,
Input,
OnDestroy,
OnInit,
TemplateRef,
AfterViewInit,
Host,
ViewContainerRef,
} from '@angular/core';
@Dornhoth
Dornhoth / info-popup.directive.ts
Last active September 2, 2020 15:42
Directive
import {
Directive,
Input,
TemplateRef,
} from '@angular/core';
@Directive({
selector: '[appInfoPopup]',
})