Angular 父组件到子组件的值在角度方向上传递不正确
我试图使用输入装饰器传递json值,但它不起作用。 我试图将数据从父组件传递到子组件,但它不能正常工作 子组件Angular 父组件到子组件的值在角度方向上传递不正确,angular,Angular,我试图使用输入装饰器传递json值,但它不起作用。 我试图将数据从父组件传递到子组件,但它不能正常工作 子组件 <div class="card"> <div class="card-body"> <h4 class="card-title">{{ analyticsTitle }}</h4> <div *ngFor="let data of datas"> <a href="{{data.
<div class="card">
<div class="card-body">
<h4 class="card-title">{{ analyticsTitle }}</h4>
<div *ngFor="let data of datas">
<a href="{{data.redirectionUrl}}" class="card-link">{{data.description}}</a>
</div>
<div class="text-right view-more">
<a href="{{viewMoreUrl}}">
{{ viewMore }}
</a></div>
</div>
</div>
<app-widget *ngFor="let j of analytics" [joke]="j"></app-widget>
父组件
<div class="card">
<div class="card-body">
<h4 class="card-title">{{ analyticsTitle }}</h4>
<div *ngFor="let data of datas">
<a href="{{data.redirectionUrl}}" class="card-link">{{data.description}}</a>
</div>
<div class="text-right view-more">
<a href="{{viewMoreUrl}}">
{{ viewMore }}
</a></div>
</div>
</div>
<app-widget *ngFor="let j of analytics" [joke]="j"></app-widget>
JSON 1
[
{
"BI-Analytics": [{
"heading": [{
"viewmore": "View More",
"viewmoreURL": "http://google.com"
}],
"body": [{
"description": "test",
"redirectionUrl": "lik"
}]
}]
}]
JSON 2
[
{
"Test": [{
"heading": [{
"viewmore": "View More",
"viewmoreURL": "http://google.com"
}],
"body": [{
"description": "test",
"redirectionUrl": "lik"
}]
}]
}]
JSON 3-具有嵌套的JSON格式
[
{
"Test1": [{
"heading": [{
"viewmore": "View More",
"viewmoreURL": "http://google.com"
}],
"body": [{
"description": "test",
"redirectionUrl": "lik"
}]
},
{
"Test2": [{
"heading": [{
"viewmore": "View More",
"viewmoreURL": "http://google.com"
}],
"body": [{
"description": "test",
"redirectionUrl": "lik"
}]
}]
{
"Test3": [{
"heading": [{
"viewmore": "View More",
"viewmoreURL": "http://google.com"
}],
"body": [{
"description": "test",
"redirectionUrl": "lik"
}]
}]
}]
以上提到的格式,需要通过。因此,我希望传递如下值:
@Input() test1:
@Input() test2
建议是否可行。看不到任何问题,但以防万一,请尝试以下方法:
<ng-container *ngFor="let j of analytics">
<app-widget [joke]="j"></app-widget>
</ng-container>
您一次只向子组件传递一个分析实例(它是分析的单个对象),因此,传递的是一个对象,而不是数组 解决方案:从父内容中删除for循环:
什么是“不起作用”?Json值没有填充代码中唯一的错误是您使用的是href而不是angular routerNo,`` Json值没有分配到for循环中,因此它没有迭代是正在填充的数组?没有仍然得到相同的“widget.component.html:4错误:找不到不同的支持对象'类型为“object”的[object object]。.NgFor仅支持绑定到数组等Iterables.at NgForOf.push../node_modules/@angular/common/fesm5/common.js.NgForOf.ngDoCheck(common.js:3152)”好的,听起来分析不像预期的那样是一个数组,但实际上是另一个数组。如果它是一个对象,那么您可以使用*ngs在对象属性上迭代,以使用keyvalue管道(需要Angular 6.1)-或者,将其转换为数组是有任何示例,或者只使用Google convert object to array javascript-返回如下答案…-您可能希望使用lodash使这样的操作更简单有可能再执行两个@Input Decoratorn吗,我有多个json文件分别传递每个组件的数据有可能吗要在input decorator中传递多个参数,您可以共享此处提到的所有不同类型的数据吗?您能否建议可以作为Inout decorator传递多个参数,