I have two angular application say A and B. Now in B application's One tab i want to load entire application A. How do i achieve this?
Please let me know how to achieve this functionality. Does angular has any special tag which i can use
One solution to your problem might be to create the tag you are asking for yourself. I implemented Angular application A in Angular application B by a technique named web component or Angular elements as stated above, see e.g. https://www.techiediaries.com/angular/angular-9-elements-web-components/
In the following steps a tag shiny-app-a is defined in application A which is used later on in application B by importing the JavaScript files of A into B and referencing them by the tag.
It works in the following environment:
The steps to make it run are:
This application additionally needs the package @angular/elements.
$ ng new App-A
$ cd  App-A
$ ng add @angular/elements
My app.modules.ts looks like this:
import { BrowserModule } from '@angular/platform-browser';
// import Injector
import { Injector, NgModule } from '@angular/core';
// import createCustomElement
import { createCustomElement } from '@angular/elements';
import { AppComponent } from './app.component';
@NgModule({
  declarations: [
  AppComponent
  ],
  imports: [
  BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {
  constructor(private injector: Injector){}
  ngDoBootstrap(){
    const el = createCustomElement(AppComponent, {injector:this.injector});
    // Here you define your new tag shiny-app-a
    customElements.define('shiny-app-a', el);
  }
 }
$ ng build --prod --output-hashing none
This generates among others the files:
You will find them in the folder [...]\App-A\dist\App-A
Run
$ ng serve
and see in the browser your newly built application A.
Note: no @angular/elements is needed here.
$ ng new App-B
In this example I use the folder [...]/src/assets.
$ cd App-B/src/assets/
and copy the JavaScript from application A to this folder, i.e.:
The crucial item to implement is CUSTOM_ELEMENTS_SCHEMA. My app.modules.ts of application B looks like this:
import { BrowserModule } from '@angular/platform-browser';
// import CUSTOM_ELEMENTS_SCHEMA
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
import { AppComponent } from './app.component';
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  // add schemas
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
2.3.2 Load JavaScript from external source in app.component.ts
The JavaScript files may also be loaded from any source. In this case the files are loaded by OnInit() from the [...]/assets folder. Thus my app.component.ts of application B looks like this:
import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'App-B';
  ngOnInit() {
    this.loadScript('/assets/runtime.js');
    this.loadScript('/assets/main.js');
    this.loadScript('/assets/polyfills.js');
  }
  public loadScript(url: string) {
    const body = <HTMLDivElement> document.body;
    const script = document.createElement('script');
    script.innerHTML = '';
    script.src = url;
    script.async = false;
    script.defer = true;
    body.appendChild(script);
  }
}
2.3.3 Use tag in app.component.html
Replace anything from the file app.component.html and just by:
<shiny-app-a></shiny-app-a>
Run
$ ng serve
and see in the browser application A in application B.
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