2

I read the Angular 2 upgrade guide and successfully bootstrapped a hybrid app (ng1 as base code and now incrementally rewriting the components and services to ng2).
Now what I don't understand is how I can use 3rd party ng1 modules, let angular2 inject them into my HeaderComponent?
I just saw examples of upgrading providers and components, but I think I'm still confused abou the module concept in angular2 so I don't get how to upgrade ng1 modules. Someone can please enlighten me?

upgrade_adapter.ts

import {AppModule} from './app.module';
import {UpgradeAdapter} from '@angular/upgrade';

export const upgradeAdapter = new UpgradeAdapter(AppModule);

app.module.ts

// imports...
@NgModule({
    imports: [ 
        BrowserModule, 
        HttpModule 
    ],
    declarations: [ 
        HeaderComponent 
    ],
    providers: [ MyService ]
})
export class AppModule { }

index.ts

import { upgradeAdapter } from './upgrade_adapter';
// more imports...

angular.module(MODULE_NAME, [
    //...
])

// HeaderComponent uses a ng1 3rd party module, how to use that module in that component
.component('header', upgradeAdapter.downgradeNg2Component(HeaderComponent))

// adding some ng1 components...

.factory('myService', upgradeAdapter.downgradeNg2Provider(MyService));

// bootstrap the application
angular.element(document).ready(function () {
    upgradeAdapter.bootstrap(document.body, [MODULE_NAME]);
});

EDIT:
The 3rd party module is written in JS.

lib/angular-modules/communication/index.js

var communicationModule = angular.module('communication', [])
    .factory('communication', require('./communication.factory'));
module.exports = communicationModule.name;

1 Answer 1

1

So I had a similar problem, basically I want to declare ng1 components in child modules. The only way I've found to do it is to use a dummy class to build the upgradeAdapter then switch it out just before calling the bootstrap method. You'll need to create new modules for Angular 2 and declare the ng1 components in them.

Hope this helps, took me ages to work out.

upgrade_adapter.ts

import {UpgradeAdapter} from '@angular/upgrade';
import { NgModule, forwardRef } from '@angular/core';

export const upgradeAdapter = new UpgradeAdapter(forwardRef(() => DummyClass)
@NgModule({})
export class DummyClass { }

app.module.ts

import { Ng1Module } from 'ng1.module';
// imports...
@NgModule({
    imports: [ 
        BrowserModule, 
        HttpModule,
        Ng1Module
    ],
    declarations: [ 
        HeaderComponent 
    ],
    providers: [ MyService ]
})
export class AppModule { }

ng1.module.ts

import { upgradeAdapter } from './upgrade-adapter'

@NgModule( {
    declarations:[upgradeAdapter.upgradeNg1Component('ng1Component'),]
})
export class Ng1Module {}

index.ts

import { upgradeAdapter } from './upgrade-adapter'
import { AppModule} from './app.module'

/** codes **/

angular.element(document).ready(function () {
    upgradeAdapter['ng2AppModule'] = AppModule;
    upgradeAdapter.bootstrap(document.body, ['app']);
});
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't work in my case as I want to upgrade a whole ng1 module, which itself contains serveral factories and requires some other 3rd party libs (e.g. angular-translate). So as there is no upgradeAdapter.upgradeNg1Module or something, I guess there is no way of reusing my existing ng1 module.
Not that I've seen, as I understand it you'd have to essentially declare the module in ng2 and redeclare all the services and components. This should still work for 3rd party libs.

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.