首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对象内的对象未传递给Angular-2 Typescript中的子组件

对象内的对象未传递给Angular-2 Typescript中的子组件
EN

Stack Overflow用户
提问于 2017-08-01 21:23:57
回答 1查看 576关注 0票数 0

在我的Angular 2.4 Typescript代码中有以下JSON对象

代码语言:javascript
复制
variableJSON = 
{
  'fQuery': string,
  'fQueryRoot': string,
  'fQueryRootURL': string,
  'fName': string,
  'filterJSON': Object
}

例如,filterJSON对象通过HTTP GET响应(基于Promise)存储:

代码语言:javascript
复制
this.service.get(URL).then(
 // whatever is returned from promise is stored in the above JSON
res => this.variableJSON['filterJSON'] = res);

我在数组中推送上述variableJSON,并将其传递给我的子组件,如下所示:

代码语言:javascript
复制
<child-comp *ngFor="let eachFilter of arrayPassingToChild" [filterProperties]="eachFilter">
<!-- assuming eachFilter is the exact variableJSON mentioned Above passed to child -->
</child-comp>

当我在父组件上执行控制台日志时,推送到数组的对象将完全显示filterJSON对象。相反,当我在控制台登录filterProperties时,它是子组件中的一个@Input()

我得到了所有的字符串。fQuery ..等等,但是filterJSON对象是完全空的。

可能的原因是什么?

推理

我假设创建的数组比HTTP GET返回的Promise更早传递,也许filterJSON是空的。

信息

子组件只是CSS框,它与上述JSON中的数据一起显示。在一般情况下,当单击特定位置时,会出现CSS框。

EN

回答 1

Stack Overflow用户

发布于 2017-08-01 21:41:47

只要你传入的是真实的数据,而不仅仅是数据的接口,你就会得到它。

这是一个可以脱掉的柱塞。

http://plnkr.co/edit/4TRbOejaUmhDmI170aPI?p=preview

应用程序组件

代码语言:javascript
复制
@Component({
  selector: 'my-app',
  template: `
    <div>App Started</div>
    <child-component [data]="point" *ngFor="let point of data"></child-component>
  `,
})
export class AppComponent {
  constructor() {
    this.data = [{
      point : 0
    },{
      point : 1
    },{
      point : 2
    }]
  }
}

子组件

代码语言:javascript
复制
@Component({
  selector: 'child-component',
  template: `
   <div>Got Data Point {{data.point}}</div>
  `,
})
export class ChildComponent {
   @Input() data;
  constructor() {
  }
}

输出

代码语言:javascript
复制
App Started
Got Data Point 0
Got Data Point 1
Got Data Point 2
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45439130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档