2016-09-28 32 views
0

我開始一個angular2項目,希望重新使用我的angular1組件。我試圖在此基礎上plunkr http://plnkr.co/edit/yMjghOFhFWuY8G1fVIEg設置我的項目,但是當我試圖運行我的應用程序,我得到這個錯誤:錯誤與混合angular1 + 2應用程序:未捕獲錯誤:UpgradeAdapter不能實例化沒有Angular 2應用程序的NgModule

Uncaught Error: UpgradeAdapter cannot be instantiated without an NgModule of the Angular 2 app. 

的plunkr剛纔實例升級適配器沒有任何NG2模塊,我不明白爲什麼它不適合我。我不確定plunkr使用的angular2版本是什麼,但我使用的是2.0.0版本。

回答

0

顯然,角2.0.0需要將NgModule傳遞給UpgradeAdapter構造函數。我挖了從@角/更新下面的代碼片段,它的工作原理:

var adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module)); 
var module = angular.module('myExample', []); 

module.directive('greet', function() { 
return { 
scope: {salutation: '=', name: '='}, 
template: '{{salutation}} {{name}}! - <span ng-transclude></span>' 
}; 
}); 

module.directive('ng2', adapter.downgradeNg2Component(Ng2)); 

@Component({ 
    selector: 'ng2', 
    template: 'ng2 template: <greet salutation="Hello" [name]="world">text</greet>' 
}) 
class Ng2 { 
} 

@NgModule({ 
declarations: [Ng2, adapter.upgradeNg1Component('greet')], 
imports: [BrowserModule] 
}) 
class MyNg2Module { 
} 

document.body.innerHTML = '<ng2></ng2>'; 

adapter.bootstrap(document.body, ['myExample']).ready(function() { 
    expect(document.body.textContent).toEqual("ng2 template: Hello world! - text"); 
}); 
+0

不知道,如果你在你的代碼有這個其他地方,但你還需要從'進口{} forwardRef「@角/核心」;' –

相關問題