I need to remove the currently running Angular application from the website and I was trying to do that by getting rid of the HTML app node. However I discovered recently side effects of doing so i.e. some callbacks that Angular installed were not removed.
What is then the proper way to tear down an Angular 4 application?
The current approach that I'm using doesn't seem to be correct:
function tryRemoveApplicationNode() {
const currentApplicationNode = document.getElementsByTagName('ngk-app')[0];
if (currentApplicationNode) {
const parent = currentApplicationNode.parentNode;
parent.removeChild(currentApplicationNode);
}
}
Angular 4 is a JavaScript framework for building web applications and apps in JavaScript, html, and TypeScript, which is a superset of JavaScript. Angular provides built-in features for animation, http service, and materials which in turn has features such as auto-complete, navigation, toolbar, menus, etc.
What is AngularJS? Angular is an opinionated JavaScript framework for building dynamic web applications. It uses templating, dependency injection, and data binding to turn HTML into an application development oriented environment.
The problem is that it might not actually improve your Angular apps considerably because you are going the wrong way around the problem. If you start with the solution and try to apply it before you have identified the problem you are most likely going nowhere.
Angular 4 Forms. In Angular 4, the following four statuses are commonly used by forms: valid – state of the validity of all form controls, true if all controls are valid. invalid – inverse of valid; true if some control is invalid. pristine – gives a status about the “cleanness” of the form; true if no control was modified.
At first you need the @angularclass/hmr
package:
npm install @angularclass/hmr
This makes your life a bit easier.
You should call destroy
on your AppModule
in the dispose
call of the hot
module and after that use the createNewHosts
method of the @angularclass/hmr
package. You can try this as a hmrBootstrap
function:
import {createNewHosts} from '@angularclass/hmr';
import {ApplicationRef, NgModuleRef} from '@angular/core';
export const hmrBootStrap: Function = async (): Promise<void> => {
module['hot'].accept();
const ngModule: NgModuleRef<AppModule> = await bootstrap();
module['hot'].dispose(() => {
const makeVisible: () => void = createNewHosts(
ngModule.injector.get(ApplicationRef).components.map(
c => c.location.nativeElement
)
);
ngModule.destroy();
makeVisible();
});
};
And have a normal bootstrap
method defined like this:
export const bootstrap: any = (): Promise<NgModuleRef<AppModule>> => {
return platformBrowserDynamic().bootstrapModule<AppModule>(AppModule);
};
And as a (partial) main.ts
you can have this:
if (module.hot) {
hmrBootStrap();
} else {
bootstrap();
}
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