Javascript 如何将API中的数据加载到Angular 2中的数组中?
所以上周我开始学习AngularJS,结果发现我最好还是学习Angular2。在大量阅读和修改Plunker上的示例应用程序之后,我终于准备好进入Angular 2。上周,我使用AngularJS创建了一个简单的应用程序,可以从API检索数据并将其转换为导航菜单。因此,本周我将尝试将该代码移植到Angular 2 我不能说这很容易,但经过多次修改后,我发现它不起作用的原因是它甚至没有提取数据。下面是我提取数据的服务代码Javascript 如何将API中的数据加载到Angular 2中的数组中?,javascript,angular,typescript,Javascript,Angular,Typescript,所以上周我开始学习AngularJS,结果发现我最好还是学习Angular2。在大量阅读和修改Plunker上的示例应用程序之后,我终于准备好进入Angular 2。上周,我使用AngularJS创建了一个简单的应用程序,可以从API检索数据并将其转换为导航菜单。因此,本周我将尝试将该代码移植到Angular 2 我不能说这很容易,但经过多次修改后,我发现它不起作用的原因是它甚至没有提取数据。下面是我提取数据的服务代码 /src/app/navigation.service.ts import
/src/app/navigation.service.ts
import { NavItem } from './navigation.model';
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class NavService {
sections: NavItem[] = [];
categories: NavItem[] = [];
constructor(private http: Http) {
}
loadSections() {
//var headers = new Headers();
//headers.append('Content-Type', 'application/json');
this.http
.get('http://localhost:15557/api/navigation/sections/list')
.map(res => {
return res.json()
})
.subscribe(
data => {
this.sections = data;
},
err => this.logError(err),
() => console.log("Loaded all sections")
);
}
loadCategories(id) {
this.http
.get('http://localhost:15557/api/navigation/categories/' + id)
.map(res => res.json())
.subscribe(
data => this.categories = [data],
err => this.logError(err),
() => console.log("Loaded categories in section with id " + id)
);
}
logError(err) {
console.error('There was an error: ' + err);
}
}
export class NavItem {
// I am never going to use int unless I need to do math operations //
id: string;
name: string;
pid: string;
slug: string;
constructor(id: string, name: string, pid: string, slug: string) {
this.id = id;
this.name = name;
this.pid = pid;
this.slug = slug;
}
}
import { Component } from '@angular/core';
import { NavItem } from './navigation.model';
import { NavService } from './navigation.service';
@Component({
selector: 'app-navigation',
templateUrl: './navigation.component.html',
styleUrls: ['./navigation.component.css']
})
export class NavComponent {
public section: NavItem;
constructor(private service: NavService) { }
ngOnInit() {
this.service.loadSections();
}
}
/src/app/navigation.model.ts
import { NavItem } from './navigation.model';
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class NavService {
sections: NavItem[] = [];
categories: NavItem[] = [];
constructor(private http: Http) {
}
loadSections() {
//var headers = new Headers();
//headers.append('Content-Type', 'application/json');
this.http
.get('http://localhost:15557/api/navigation/sections/list')
.map(res => {
return res.json()
})
.subscribe(
data => {
this.sections = data;
},
err => this.logError(err),
() => console.log("Loaded all sections")
);
}
loadCategories(id) {
this.http
.get('http://localhost:15557/api/navigation/categories/' + id)
.map(res => res.json())
.subscribe(
data => this.categories = [data],
err => this.logError(err),
() => console.log("Loaded categories in section with id " + id)
);
}
logError(err) {
console.error('There was an error: ' + err);
}
}
export class NavItem {
// I am never going to use int unless I need to do math operations //
id: string;
name: string;
pid: string;
slug: string;
constructor(id: string, name: string, pid: string, slug: string) {
this.id = id;
this.name = name;
this.pid = pid;
this.slug = slug;
}
}
import { Component } from '@angular/core';
import { NavItem } from './navigation.model';
import { NavService } from './navigation.service';
@Component({
selector: 'app-navigation',
templateUrl: './navigation.component.html',
styleUrls: ['./navigation.component.css']
})
export class NavComponent {
public section: NavItem;
constructor(private service: NavService) { }
ngOnInit() {
this.service.loadSections();
}
}
/src/app/navigation.component.ts
import { NavItem } from './navigation.model';
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class NavService {
sections: NavItem[] = [];
categories: NavItem[] = [];
constructor(private http: Http) {
}
loadSections() {
//var headers = new Headers();
//headers.append('Content-Type', 'application/json');
this.http
.get('http://localhost:15557/api/navigation/sections/list')
.map(res => {
return res.json()
})
.subscribe(
data => {
this.sections = data;
},
err => this.logError(err),
() => console.log("Loaded all sections")
);
}
loadCategories(id) {
this.http
.get('http://localhost:15557/api/navigation/categories/' + id)
.map(res => res.json())
.subscribe(
data => this.categories = [data],
err => this.logError(err),
() => console.log("Loaded categories in section with id " + id)
);
}
logError(err) {
console.error('There was an error: ' + err);
}
}
export class NavItem {
// I am never going to use int unless I need to do math operations //
id: string;
name: string;
pid: string;
slug: string;
constructor(id: string, name: string, pid: string, slug: string) {
this.id = id;
this.name = name;
this.pid = pid;
this.slug = slug;
}
}
import { Component } from '@angular/core';
import { NavItem } from './navigation.model';
import { NavService } from './navigation.service';
@Component({
selector: 'app-navigation',
templateUrl: './navigation.component.html',
styleUrls: ['./navigation.component.css']
})
export class NavComponent {
public section: NavItem;
constructor(private service: NavService) { }
ngOnInit() {
this.service.loadSections();
}
}
这个代码有什么错 我以前从未使用过这样的服务。也许您的实现有一个简单的错误,但以下是我如何始终在angular中实现服务: 服务 组成部分
我以前从未使用过这样的服务。也许您的实现有一个简单的错误,但以下是我如何始终在angular中实现服务: 服务 组成部分
乍一看,代码似乎不错。我建议使用Chrome开发工具查看您的网络流量,看看您得到了什么样的响应。另外,我将
console.log
记录订阅中的数据
响应,以了解实际发生的情况。另外,确保您实际使用的是Angular 2。在Angular 4中,他们改变了您执行HTTP请求的方式。如果您不确定,请按照Targaryen所说的做,并尽可能多地插入console.log
,以查看流程。我注意到的一件事是,您没有任何机制将部分
数据从服务返回到您的组件,您看到的是正确的位置吗?乍一看,代码似乎很好。我建议使用Chrome开发工具查看您的网络流量,看看您得到了什么样的响应。另外,我将console.log
记录订阅中的数据
响应,以了解实际发生的情况。另外,确保您实际使用的是Angular 2。在Angular 4中,他们改变了您执行HTTP请求的方式。如果您不确定,请按照Targaryen所说的做,并尽可能多地插入console.log
,以查看流程。我注意到的一件事是,您没有任何机制将部分
数据从服务返回到您的组件,您找对地方了吗?