Am I getting this wrong or should style nodes disappear from the head of the document when a component is destroyed? https://github.com/juleskremer/angular/commit/385ed90ac373c0347ea88fe38685405c01ba1a58
If I set encapsulation to "none" the style node added for that component remains even if it is destroyed?
Is there a way of deleting the style node when component gets destroyed?
Steps to delete a component in Angular. Remove the import line reference from Angular app.module.ts file. Remove the component declaration from @NgModule declaration array in app.module.ts file; And then manually delete the component folder from Angular project. Finally Delete all the references of component manually from the Angular project.
Angular provides NgStyle directive that helps us to style specific components. Of course, you can style single or multiple components, but what is NgStyle? Let’s deep dive into that. Angular NgStyle is a built-in directive that lets you set a given DOM element’s style properties.
Note: I tried the example in both Angular 5 and Angular 6. If using 'encapsulation: ViewEncapsulation.None' on an Angular component a <style> element will be appended to the <head> when component is being shown. The <style> element will never get removed, even after the component is destroyed.
When we create a component in Angular, there are multiple ways we consume such an observable and hence we need to be mindful of how we dispose of these observables when the component goes out of scope/view. Angular provides the ngOnDestroy lifecycle hook (in addition to the async pipe) which you can use to accomplish exactly these types of tasks.
Angular does not support to removeing style node from document. But I write an Angular Provider named "StyleService" for create/remove a style node from document.
Service: style.service.ts
import { Injectable, Inject, Optional } from '@angular/core';
import { STYLE_HOST } from 'app/common';
@Injectable()
export class StyleService {
private stylesMap: Map<any, Node> = new Map();
constructor(
@Optional() @Inject(STYLE_HOST) private host: Node
) {
if (host === null) {
this.host = document.head;
}
}
createStyleNode(content: string): Node {
const styleEl = document.createElement('style');
styleEl.textContent = content;
return styleEl;
}
has(key: any): boolean {
return this.stylesMap.has(key);
}
addStyle(key: any, style: string): void {
const styleEl = this.createStyleNode(style);
this.stylesMap.set(key, styleEl);
this.host.appendChild(styleEl);
}
removeStyle(key: any): void {
const styleEl = this.stylesMap.get(key);
if (styleEl) {
this.stylesMap.delete(key);
this.host.removeChild(styleEl);
}
}
}
Component: login.component.ts
You need to create a css file in same folder, and require it.
export class LoginComponent implements OnDestroy {
constructor(
private styleService: StyleService
) {
styleService.addStyle('loginStyle', require('./style.css'));
}
ngOnDestroy(): void {
this.styleService.removeStyle('loginStyle');
}
}
Here is my opinion on this.
Think that there are 1000 list elements and on click, you are moving to details component for each list element. So if you remove styles on destroying of details component, what if you redirect to it again for other list elements. Every time loading CSS is a burden. Maybe for that reason, it is not getting removed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With