2016-12-29 59 views
0

在我的Angular 2應用程序中,我需要將數組從一個組件傳遞到另一個組件。我有一個名爲SystemDynamicsComponent組件,公開了一個數組sdFactors異常self.context.XXX.subscribe不是函數

@Component({ 
    selector: "system-dynamics", 
    templateUrl: "/app/component/main/template/system-dynamics.html" 
}) 

export class SystemDynamicsComponent implements OnInit { 
    private dialogDisplayed: boolean = false; 
    @Output() sdFactors: any[] = []; 

名爲InputModuleComponent我需要如下讀取此值的組件:

模板:

<system-dynamics (sdFactors)="this.sdFactors"></system-dynamics> 

組件:

export class InputModuleComponent implements OnInit { 
... 
sdFactors: any[] = []; 

當launchig輸入模塊組件,我收到來自Angular的此錯誤消息: TypeError: self.context.sdFactors.subscribe is not a function at Wrapper_SystemDynamicsComponent.subscribe (wrapper.ngfactory.js:38) at View_InputModuleComponent6.createInternal (component.ngfactory.js:3103) at View_InputModuleComponent6.AppView.create (core.umd.js:12262) at View_InputModuleComponent6.DebugAppView.create (core.umd.js:12666) at TemplateRef_.createEmbeddedView (core.umd.js:9320) at ViewContainerRef_.createEmbeddedView (core.umd.js:9552)

任何想法我失蹤?

回答

2

試試這個

<system-dynamics (sdFactors)="sdFactors"></system-dynamics> 

代替

<system-dynamics (sdFactors)="this.sdFactors"></system-dynamics> 

你不需要在模板中添加this的結合。模板中沒有this可以訪問同一類的變量。

更新

@output用於事件不結合爲屬性綁定,則必須使用@input爲P將陣列或一些變量的值,從而使用此這樣

<system-dynamics [sdFactors]="sdFactors"></system-dynamics> 

和在這樣你部件

@Component({ 
    selector: "system-dynamics", 
    templateUrl: "/app/component/main/template/system-dynamics.html" 
}) 

export class SystemDynamicsComponent implements OnInit { 
    private dialogDisplayed: boolean = false; 
    @Input() sdFactors: any[] = []; 
+0

我改變了你的想法(''system-dynamics(sdFactors)=「sdFactors」>'),但問題依然存在。 – Emdee

+0

請看我的更新 –

+0

問題解決了!按照您的建議,我現在使用'@ Input'。 – Emdee

相關問題