import { Component , OnDestroy , OnInit } from '@angular/core' ; import { ReplaySubject , Subject , Subscription } from 'rxjs' ; import { CategoryService } from '../../services/category.service' ; import { TypeService } from '../../services/type.service' ; import { CmdbMode } from '../../modes.enum' ; import { ActivatedRoute , Params } from '@angular/router' ; import { CmdbCategory } from '../../models/cmdb-category' ; import { takeUntil } from 'rxjs/operators' ; @ Component ( { selector : 'cmdb-category-view' , templateUrl : './category-view.component.html' , styleUrls : [ './category-view.component.scss' ] } ) export class CategoryViewComponent implements OnInit , OnDestroy { public mode : CmdbMode = CmdbMode . View ; private unSubscribe : ReplaySubject < void > = new ReplaySubject ( ) ; public publicID : number ; private publicIDSubject : Subject < number > = new Subject < number > ( ) ; public category : CmdbCategory ; constructor ( private categoryService , private typeService , private route ) { this . route . params . pipe ( takeUntil ( this . unSubscribe ) ) . subscribe ( ( params ) => { this . publicIDSubject . next ( params . publicID ) ; } ) ; } public ngOnInit ( ) { this . publicIDSubject . asObservable ( ) . pipe ( takeUntil ( this . unSubscribe ) ) . subscribe ( ( publicID ) => { this . publicID = publicID ; this . categoryService . getCategory ( this . publicID ) . pipe ( takeUntil ( this . unSubscribe ) ) . subscribe ( ( category ) => { this . category = category ; console . log ( this . category ) ; } ) ; } ) ; } public ngOnDestroy ( ) { this . unSubscribe . next ( ) ; this . unSubscribe . complete ( ) ; } }