在我的Angular 2应用程序中,我需要将一个数组从一个组件传递到另一个组件。我有一个名为SystemDynamicsComponent
的组件,它公开了一个数组sdFactors
:
@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[] = [];
当启动输入模块组件时,我从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)
我缺少什么想法?
答案 0 :(得分:2)
试试这个
<system-dynamics (sdFactors)="sdFactors"></system-dynamics>
而不是
<system-dynamics (sdFactors)="this.sdFactors"></system-dynamics>
您不需要在模板中添加this
进行绑定。可以在模板中没有this
的情况下访问同一类中的变量。
@output
用于事件绑定而不是属性绑定,你必须使用@input
作为passign数组或一些变量值,所以像这样使用
<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[] = [];