我有两个选择下拉列表。第一个列出域,第二个列出属于域。当我选择域时,将发送域的ID。 API由Laravel生成的json提供。如果categories对象中存在匹配项,则应返回那些匹配项(如果发送的id等于activity_domain_id)。
从应用程序组件中选择方法
seleDomain(domainId: number) {
this.formsService.getDomains()
.then(categories => this.categories.filter(item => item.activity_domain_id === domainId))
.catch(error => this.error = error);
}
这就是Categories和Domain类的样子:
export class Domain {
id: number;
user_id: number;
name: string;
}
export class Categories {
id: number;
activity_domain_id: number;
name: string;
}
从服务组件获取域并获取类别方法
getDomains(domain: Domain): Promise<Domain[]> {
return this.http.get(this.domainsUrl)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
getCategories(userId: number): Promise<Categories[]> {
return this.http.get(this.categoriesUrl)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || { };
}
以下是extractData返回的域json如下所示:
[{"id":1,"user_id":1,"name":"I.T","updated_at":"2016-10-06 14:07:55","created_at":"2016-03-03 19:28:54"},{"id":3,"user_id":1,"name":"Management","updated_at":"2016-10-06 16:27:18","created_at":"2016-08-09 15:10:34"}]
以下是extractData返回的类别json如下所示:
[{"id":9,"activity_domain_id":1,"name":"Front end developer","updated_at":"2016-09-06 10:19:23","created_at":"2016-09-05 15:21:08"},{"id":10,"activity_domain_id":1,"name":"Back end developer","updated_at":"2016-09-05 15:21:20","created_at":"2016-09-05 15:21:20"}]
进行过滤时,不会返回匹配项。