【发布时间】:2018-08-27 12:20:08
【问题描述】:
我有这个组件:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-workers-edit',
templateUrl: './workers-edit.component.html',
styleUrls: ['./workers-edit.component.css']
})
export class WorkersEditComponent implements OnInit {
public workerid;
public lastname;
public address;
public phone;
public email;
workers = [];
constructor(private route: ActivatedRoute, private http: HttpClient) { }
ngOnInit() {
let id = parseInt(this.route.snapshot.paramMap.get('id'));
this.workerid = id;
let Workerobs = this.http.get('http://myurlPP/cfcs/workernameapi.cfm');
Workerobs.subscribe((res:any)=> {
console.log(res);
this.workers = res;
});
}
}
在我的 html 中,如果我这样做:
<ul>
<li *ngFor="let name of workers">{{name.WNAME}}</li>
</ul>
我会从数组中获取所有的名字并显示出来,没关系。
但我只需要显示匹配workerid 的名称。 (我有 workerid 因为 let id = parseInt(this.route.snapshot.paramMap.get('id')); this.workerid = id;
那么如何从workers数组中提取WID,将其与workerid进行比较,如果为真,则只显示WNAME(工人姓名) ?
【问题讨论】:
-
你不能只用 res.filter(......) 在 Workerobs.subscribe 方法中过滤吗??
-
不使用过滤器或 *ngFor,使用 find 和变量 this.myuser=workers.find(w=>w.id==id);
标签: javascript arrays angular typescript