2016-10-04 26 views
0

我有一個小方法,使用Compiler('@angular/core')compileModuleAndAllComponentsAsync()方法創建一個組件。當我創建組件,具有下列錯誤沒有提供當compileModuleAndAllComponentsAsync()angular2 - 2.0最終版本

無提供者名稱服務「

我知道這是什麼錯誤,但我嘗試編譯模塊有這個提供商:!

@NgModule({ 
    imports: [CommonModule, RouterModule, FormsModule], 
    declarations: [MyComponent], 
    providers: [NameService] 
}) 
export class MyModule {} 

所以,我怎麼能在組件上注入提供商NameService和其他供應商正在創建?

this.compiler.compileModuleAndAllComponentsAsync(this._createDynamicComponent()) 
.then(factory => { 
    const compFactory = factory.componentFactories.find((x: any) => x.componentType === this.instanceComponent()); 
    const cmpRef = this.vcRef.createComponent(compFactory); 

    this.addToInstance(cmpRef, (cmpRef) => { 
     cmpRef.destroy(); 
    }); 
}); 

回答

0

經過數小時尋找代碼,我發現問題是因爲Component沒有provider:[NameService]。我插入了提供程序,並都正常工作。

我花了很長時間才發現這個問題,因爲組件工作正常,只有當我嘗試動態創建它時,就是問題發生了。

相關問題