Search code examples
angularjsangularangular-upgradeangular-hybrid

Angular 8 hybrid app not recognizes AngularJS components


I started developing a hybrid application. So I have done th folllowing steps:

  • add Angular 8 dependencies
  • add polyfills.ts
  • remove ng-app attribute from my root index.html
  • do a manual bootstrap of AngularJs app

How looks my Angular init module:

@NgModule({
    imports: [
        BrowserModule,
        UpgradeModule
    ]
})
export class HubAngularModule {
    ngDoBootstrap() {
    }
}

platformBrowserDynamic().bootstrapModule(HubAngularModule)
    .then(platformRef => {
        console.log("Bootstrapping in Hybrid mode with Angular & AngularJS");
        const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;

        upgrade.bootstrap(document.body, ['myAngularJsModule']);
    });

How looks my index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <script src="dist/index.bundle.js"></script> <!--Webpack bundle-->
  <link rel="stylesheet" href="dist/styles.css"/>
</head>
<body>
<div layout="column" layout-align="" ng-cloak>
    <main-header></main-header> <!--AngularJS header component-->
    <console-menu></console-menu> <!--AngularJS menu component-->
    <md-content ui-view="main"></md-content> <!--AngularJS root ui-view-->
</div>
</body>
</html>

main-header, console-menu - are AngularJS components. Of course that configuration works well when ng-app is presented.

What I expect. Hybrid app starts just like old AngularJS app and I'm able to see login page, start page etc.

What I actually got. AngularJS app is actually bootstrapping. I can see method app.module().run(...) executes. But no component loads so I see only a blank page.


Solution

  • After a several hours of experiments I have found a soultion. I decided to check whether the manual bootstrap of the AngularJS will work:

    angular.element(document)
        .ready(() => {
            angular.bootstrap(document.body, ['myAngularJsModule']);
        });
    

    And it had failed. Even if no Angular 8 present. So the reason it fails to start is a location of <script> tags with my webpack bundles. It located in <head> but should be located in <body> after all the app markup.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
    
    </head>
    <body>
    <div layout="column" layout-align="" ng-cloak>
        <main-header></main-header> <!--AngularJS header component-->
        <console-menu></console-menu> <!--AngularJS menu component-->
        <md-content ui-view="main"></md-content> <!--AngularJS root ui-view-->
    </div>
    <script src="dist/index.bundle.js"></script> <!--Webpack bundle-->
    <link rel="stylesheet" href="dist/styles.css"/>
    </body>
    </html>
    

    It is so dumb, but exactly that dumb thing like this make the biggest headache sometimes. And what disappointed me me the most not a single migration tutorial tells nothing about that detail!