简体   繁体   中英

angular2 upgradeAdapter: get reference of ng1-directive from the UpgradeAdapter?

I can add angular2 providers easy to the UpgradeAdapter:

import { HTTP_PROVIDERS } from 'angular2/http';
upgradeAdapter.addProvider(HTTP_PROVIDERS);

I can upgrade an ng1 directives (component) also quite easy:

const DirectiveNg1 = upgradeAdapter.upgradeNg1Component('DirectiveNg1');

But I need DirectiveNg1 in many places and would like to use them in angular2 components. Is it somehow possible to get a reference back?

At the moment I have specified the following in my angular2 component and it works, but I'd like to only upgradeNg1Component('DirectiveNg1') once in me main.js file.

const DirectiveNg1 = upgradeAdapter.upgradeNg1Component('DirectiveNg1');
// how can I replace the line above, and get a reference from the UpgradeAdapter
@Component({
    directives: [DirectiveNg1],
    ...
})

I know this post is a little old but this took me ages of internet searching to work out. I don't think the documentation for this is very good.

This simplest way is if you're just going to have one module. you can define it as follows.

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

export const upgradeAdapter = new UpgradeAdapter(forwardRef(() => AppModule ));
const DirectiveNg1 = upgradeAdapter.upgradeNg1Component('DirectiveNg1');

@NgModule({
 imports: [BrowserModule, HttpModule],
 providers: [],
 declarations: [DirectiveNg1],
 exports: []
})
export class AppModule { }

now DirectiveNg1 can be used in angular2 components.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM