【问题标题】:Need to pass data to child component using Form group in angular 2?需要使用 Angular 2 中的表单组将数据传递给子组件吗?
【发布时间】:2017-07-11 13:49:37
【问题描述】:

我正在尝试使用@Input 将数据从父组件传递到子组件,其中父组件和子组件都使用表单组,并且子组件通过@Input 访问控件,而如果我需要将一些数据传递给子组件使用 @input 装饰器的组件出现错误

父组件.html

 <rulebuilder [mydata]="ruledata" [generalrulegroup]="editRulesFrmGrp.controls.generalRule.controls[i]"></rulebuilder>

子组件.ts

 @Input('generalrulegroup')

@Input()mydata:any;

控制台错误

Error: Template parse errors:
Can't bind to 'generalrulegroup' since it isn't a known property of 'modifygeneralrulebuilder'. ("<modifygeneralrulebuilder [mydata]="modifyRuleJson" [ERROR ->][generalrulegroup]="editRulesFrmGrp.controls.generalRule.controls[i]"></modifygeneralrulebuilde"): RuleEditComponent@108:128
    at SyntaxError.BaseError [as constructor] (http://localhost:4200/main.bundle.js:103798:27)
    at new SyntaxError (http://localhost:4200/main.bundle.js:9270:16)
    at TemplateParser.parse (http://localhost:4200/main.bundle.js:27281:19)
    at JitCompiler._compileTemplate (http://localhost:4200/main.bundle.js:65409:68)
    at http://localhost:4200/main.bundle.js:65292:62
    at Set.forEach (native)
    at JitCompiler._compileComponents (http://localhost:4200/main.bundle.js:65292:19)
    at createResult (http://localhost:4200/main.bundle.js:65175:19)
    at ZoneDelegate.invoke (http://localhost:4200/main.bundle.js:164194:26)
    at Zone.run (http://localhost:4200/main.bundle.js:164076:43)

【问题讨论】:

    标签: angular


    【解决方案1】:

    试试这个:

    @Input('generalrulegroup') generalrulegroup: any;
    

    你需要在当前组件中声明它。

    【讨论】:

    • 当前组件表示子组件或父组件
    • 您说的是generalrulegroup,但我需要它用于mydata
    • 您的错误与generalrulegroup 有关,因此,我看到它实际上并未声明。对于当前组件,我的意思是 ChildComponent。我没有看到完整的组件代码,所以我很难看到整体发生了什么。
    • 是否可以通过表单组任意链接将数据传递给子组件
    • 您可以使用@Input() 将数据从父级传递给子级。这是链接:angular.io/guide/…
    猜你喜欢
    • 2017-08-30
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 2017-06-15
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    相关资源
    最近更新 更多