-1

我尝试过滤数据(获取给定数字的记录)

http.get(baseUrl + 'api/Student/Students1')
.filter( stud => stud.Phone == 123)
.subscribe(data => {
this.studentTEST = data.json() as Students[];
}, error => console.error(error));

但得到错误:

““响应”类型上不存在属性“电话””。

Angular 的版本是 4.2.5,但最近在 package.json (5.1.0) 中进行了更新。寻找

我从 asp.net 核心控制器获取数据库上下文。

这是我的角度代码:

import { Component, Inject } from '@angular/core';
import 'rxjs/Rx';
import { Http, Response } from '@angular/http';
import { Observable } from 'RxJS';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';


@Component({
    selector: 'fetchdata',
    templateUrl: './fetchdata.component.html'
})
export class FetchDataComponent {

    public studentTEST: Students[];
    public stud: Students[];

    constructor(private http: Http, @Inject('BASE_URL') baseUrl: string) {

    http.get(baseUrl + 'api/Student/Students1')
    .map((response: Response) => response.json() as Students[])
    .filter(stud => stud.Phone === 123);
    //subscribe(result => {
        //this.studentTEST = result.json();
 }


}

export interface Students {
     StdName: string;
     Email: string;
     Phone: number;
     Address: string;
 }

现在错误:

类型“学生 []”上不存在属性“电话”。

4

2 回答 2

1

后一个错误是完全正确的:

Property 'Phone' does not exist on type 'Students[]'.

它没有。Phone存在于Students,不存在Students[]

您实际上需要filter在地图中设置:

http.get(baseUrl + 'api/Student/Students1')
 .map(response => response.json().filter(stud => stud.Phone === 123))

那么你在这里返回的,不再是类型的响应,Students[]所以你需要把它排除在外。

于 2017-12-17T11:56:10.593 回答
1

首先,您需要对mapjson() 或使用httpClient. 然后您可以过滤并从服务中删除您的订阅方法。您需要订阅可以使用| async管道的组件。

http.get(baseUrl + 'api/Student/Students1') .map((response: Response) => response.json() as Students[]) .filter(stud => stud.Phone === 123);

于 2017-12-11T13:04:36.500 回答