我想从atom.service中得到结果数组到atom.component。我以为我可以将结果数组带入atom.component中,源代码如下(不是整个代码)。但是我发现我无法从下面的源代码访问atom.component中的this.atoms。 atom.service.ts中的结果数组已成功创建。如果有人知道如何访问atom.component中的结果数组,你可以提供一些指导吗?如何将角色2中的对象从服务返回到组件?
atom.service.ts
getAtoms(private newConcept: string) {
this.http.get('api/atoms.json')
.map((responseData) => {return responseData.json()})
.subscribe(data => {
let results:Array<RAtom> = [];
for (i = 0; i < data.result.length; i++) {
results.push(new RAtom(data.result[i].classType, data.result[i].ui));
}
return results;
});
}
atom.component.ts
atoms: Array<RAtom>;
searchAtoms(newConcept: string) {
if (newConcept) {
this.atoms = this.service.getAtoms(newConcept);
}
}
RAtom.ts
export class RAtom {
classType: string;
ui: string;
constructor(classType: string, ui:string) {
this.classType = classType;
this.ui = ui;
}
}
订阅的组件,而不是服务。 Angular 2文档充满了这个例子。 – jonrsharpe
首先,我可以问他们有什么不同,我大致了解,但我可以问吗? –
我不明白你想问什么。 – jonrsharpe