【发布时间】:2018-08-13 19:53:45
【问题描述】:
所以,我正在做一个有角度的项目。我正在通过 HTTP 调用获取数据。
为了验证我所做的事情是否正确,我通常会在控制台上将它们打印出来。当我尝试使用 console.log() 打印我的结果时,它只适用于我的方法中的对象。当我超出该方法的范围时,console.log() 不再起作用并打印出一个没有值的数组。我认为我对闭包的理解是错误的。
无论如何,我真正的问题是我从 JSON 文件中获取数据并将它们转换为两个单独的数组,然后我试图合并这两个数组。
这是我正在使用的服务:
import { Http } from '@angular/http/';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
@Injectable()
export class ManagersService {
list$;
contacts$: Array<any>;
items$;
nameDatabase: { name: string, id: number }[] = [] ;
emailDatabase = [];
managersList;
// tslint:disable-next-line:max-line-length
private url = '';
//I am hidden the URL, because it contains private data,
but it is a complex nested JSON object
constructor(private http: Http) {
}
getList() {
return this.http.get(this.url)
.map(response => response.json());
}
getManagersNames() {
this.getList()
.subscribe(list => {
this.list$ = list.data;
//console.log(this.list$);
for ( const l of this.list$) {
const managerName = l.attributes.name;
const managerId = parseInt(l.id);
const manager = { name: managerName, id: managerId };
this.nameDatabase.push(manager);
}
});
const x = this.nameDatabase;
return x;
}
checkId() {
this.getList()
.subscribe(list => {
this.contacts$ = list.included;
console.log(this.contacts$);
for ( const c of this.contacts$) {
const managerId = parseInt(c.id) - 1;
return managerId;
}
});
}
getManagersContacts() {
this.getList()
.subscribe(list => {
this.contacts$ = list.included;
// console.log(this.contacts$);
for ( const c of this.contacts$) {
if (c.attributes.email) {
const managerEmail = c.attributes.email;
const managerId = parseInt(c.id) - 1;
const manager = { email: managerEmail, id: managerId };
this.emailDatabase.push(manager);
} else {
const managerName = c.attributes.name;
const managerId = parseInt(c.id);
const manager = { name: managerName, id: managerId };
this.nameDatabase.push(manager);
}
}
});
const y = this.emailDatabase;
return y;
}
}
这是我正在使用的组件:
import { ManagersService } from './../services/managers.service';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-list-items',
templateUrl: './list-items.component.html',
styleUrls: ['./list-items.component.css']
})
export class ListItemsComponent implements OnInit {
// tslint:disable-next-line:max-line-length
constructor(private service: ManagersService) {
}
ngOnInit() {
const x = this.service.getManagersContacts();
console.log(x);
const y = this.service.getManagersNames();
console.log(y);
const hash = new Map();
x.concat(y).forEach(function(obj) {
hash.set(obj.id, Object.assign(hash.get(obj.id) || {}, obj));
});
const finalDatabase = Array.from(hash.values());
console.log(finalDatabase);
return finalDatabase;
}
}
最后,我的 app.module.ts:
import { ManagersService } from './services/managers.service';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { ListItemsComponent } from './list-items/list-items.component';
import { SearchListComponent } from './search-list/search-list.component';
import { DropDownComponent } from './drop-down/drop-down.component';
@NgModule({
declarations: [
AppComponent,
ListItemsComponent,
SearchListComponent,
DropDownComponent
],
imports: [
BrowserModule,
HttpModule
],
providers: [
ManagersService
],
bootstrap: [AppComponent]
})
export class AppModule { }
【问题讨论】:
-
你到底期待什么?
-
能够合并两个数组。当我 console.log(finalDatabase);最终数组的结果,它返回一个空的。 @Vithubati
-
欢迎来到 Stackoverflow!您缺少的关键基本概念是 http 是异步的。这意味着您将无法在调用请求后立即控制台注销数据。您必须等到该数据返回。
-
再次感谢 DeborahK。 !
标签: angular typescript