1

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.

2 Answers 2

2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Holy heck, thank you for this. The things people assume the reader knows, I guess! Now that the hybrid is finally working, I can move on to the fun part, upgrading all the things :D
0

Have you upgraded your angularJS components to make them work in Angular ?

3 Comments

Answer depends on what is 'upgrade" means. All my components are exactly Components. They defined like angular.module().component and written on Typescript.
If you can provide a stackblitz, I can help you better, What I was mentioning above was have you used upgrade component in angularJs component something like this ? ` export class AngularJSComponent extends UpgradeComponent { @Input() twoWay: number; @Output() twoWayChange: EventEmitter<number>; constructor(ref: ElementRef, inj: Injector) { super('angularjsComponent', ref, inj); } ` }
No i didn't do it. My app has near 120 AngularJS components (.ts files). Does it mean I have to apply UpgradeComponent to all of them to just a start an application? It is just a madness.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.