Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 - how to properly tear down the application?

Tags:

angular

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);
  }
}
like image 447
kubal5003 Avatar asked Nov 13 '17 08:11

kubal5003


People also ask

What is the use of Angular 4?

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?

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.

Why can't I improve my angular app?

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.

What are the forms statuses in Angular 4?

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.


1 Answers

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();
}
like image 146
Poul Kruijt Avatar answered Sep 23 '22 01:09

Poul Kruijt