I want alerts to appear within/above my static bootstrap v4 navbar.
So I've got this simple service:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class AppService {
private _navbarPadding: number = 50;
navbarPaddingChange: Subject<number> = new Subject();
constructor() {
this.navbarPadding = this._navbarPadding;
}
get navbarPadding(): number {
return this._navbarPadding;
}
set navbarPadding(val: number) {
this._navbarPadding = val;
this.navbarPaddingChange.next(this._navbarPadding);
}
}
Which I inject everywhere, including sidebar (below) and 'main body':
import { Component, OnInit, OnDestroy } from '@angular/core';
import { AppService } from '../app.service';
import { Subscription } from 'rxjs';
@Component({
selector: 'app-sidebar',
templateUrl: './sidebar.component.html',
styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit, OnDestroy {
navbarPadding: number;
subNavbarPadding: Subscription;
constructor(public appService: AppService) {}
ngOnInit() {
this.navbarPadding = this.appService.navbarPadding;
this.subNavbarPadding = this.appService.navbarPaddingChange.subscribe(val =>
this.navbarPadding = val
);
}
ngOnDestroy() {
this.subNavbarPadding.unsubscribe();
}
}
Then I've got this function:
addAlert() {
this.appService.navbarPadding += 81;
this.alertsService.alerts.push({
type: 'info',
msg: 'INFO'
})
}
Sidebar html (first line):
<div class="col-sm-3 col-md-2 sidebar" [style.margin-top.px]=navbarPadding>
…and it works just fine. But… this must be a terrible idea. It has heavy coupling everywhere. What's the correct Angular2 approach?