Javascript Angular2将JSON对象传递给组件的构造函数

Javascript Angular2将JSON对象传递给组件的构造函数,javascript,json,angular,electron,Javascript,Json,Angular,Electron,我的第一个组件config.editor.component执行大量处理并创建各种不同的JSON对象 我对这种方法很满意。然后,此组件的模板使用*ngFor循环呈现另一个组件-api.entry.component。如何将在config.editor.component.ts中创建的JSON对象传递到api.entry.component的构造函数中 我当前的模板如下所示: <!-- loop and display all api components here --> <c

我的第一个组件
config.editor.component
执行大量处理并创建各种不同的JSON对象

我对这种方法很满意。然后,此组件的模板使用*ngFor循环呈现另一个组件-
api.entry.component
。如何将在config.editor.component.ts中创建的JSON对象传递到
api.entry.component
的构造函数中

我当前的模板如下所示:

<!-- loop and display all api components here -->
<ct-api-entry *ngFor="let api in apis"></ct-api-entry>
我真正想要的是能够从api.entry.component.ts的构造函数中的模板循环访问特定的
api

import { Component } from '@angular/core';

@Component({
  selector: 'ct-api-entry',
  templateUrl: 'api.entry.component.html',
  styleUrls: ['api.entry.component.css']
})
export class APIEntryComponent {
//I NEED TO PASS THIS CONSTRUCTOR AN SPECIFIC JSON OBJECT FROM APIS IN THE CONFIG-EDITOR MAIN COMPONENT
  constructor() {
 //I WANT TO ACCESS MY JSON API OBJ IN HERE!
  }

}

我觉得这一定很简单,但我很难看到它是如何工作的!请帮助我了解如何将*ngFor循环中的JSON对象传递到模板正在构建的组件的构造函数中。

使用
ct api条目中的
输入
装饰器定义
api

import { Component, Input } from '@angular/core';

@Component({
  selector: 'ct-api-entry',
  templateUrl: 'api.entry.component.html',
  styleUrls: ['api.entry.component.css']
})
export class APIEntryComponent {
   @Input() api: any;   
   constructor() {}
}
然后将其传递到模板中:

<ct-api-entry *ngFor="let api of apis" [api]="api"></ct-api-entry>

ct api条目
组件中使用
输入
装饰程序定义
api

import { Component, Input } from '@angular/core';

@Component({
  selector: 'ct-api-entry',
  templateUrl: 'api.entry.component.html',
  styleUrls: ['api.entry.component.css']
})
export class APIEntryComponent {
   @Input() api: any;   
   constructor() {}
}
然后将其传递到模板中:

<ct-api-entry *ngFor="let api of apis" [api]="api"></ct-api-entry>


我对angular2还不太熟悉,我想你可以通过服务来实现你想要的吗?具体来说,是一个共享服务,其中一个组件通过&将数据保存到该服务,并使用另一个组件从共享服务检索存储的数据,但我认为您指的是JavaScript对象。JSON是对象序列化。我对angular2还比较陌生,我想你会通过服务实现你想要的吗?具体来说,是一个共享服务,其中一个组件通过&将数据保存到该服务,并使用另一个组件从共享服务检索存储的数据,但我认为您指的是JavaScript对象。JSON是对象序列化。