访问已接收数据的值

时间:2019-02-13 20:17:50

标签: angular

如何访问在控制台中收到的数据值?

如何在这里访问两个数组元素的标题? 我必须过滤列表acc。到所有数组元素的标题,所以我需要访问元素的所有标题 这是控制台的ss

https://ibb.co/tq8K67k

component.ts文件

export class AdminProductsComponent implements OnInit {
@ViewChild('f') searchform:NgForm
onsubmit(){

}
search=''
itemlist

onclick(){
this.search=this.searchform.value
this.itemlist=this.prservice.getallproducts()
console.log(this.itemlist)


}





headElements = ['S.No' ,'Title', 'Price'];




 listofproducts
 productreceived

constructor(private prservice:Productservice,private 
route:ActivatedRoute,private router:Router) { }

ngOnInit() {
this.listofproducts=this.prservice.getallproducts()

}
onclickedit(id){
// console.log(id)
this.router.navigate([id],{relativeTo:this.route})

}


 }

service.ts文件

export class Productservice{
updatedproduct=new BehaviorSubject<any>(1)
card=new Subject<any>()
cards=[]

addtocards(value){
    this.cards.push(value)
}
getallproducts(){
    return this.cards
}

getproductbyid(id){
    return this.cards[id]
}

updateproduct(id,product){
    this.cards[id]=product
    // console.log(this.cards[id])
    this.updatedproduct.next(this.cards[id])
}
deleteproduct(id){
    alert('Are you sure you want to delete this product permanently 
? Once deleted will not revert back')
    this.cards.splice(id,1)
    this.updatedproduct.next(this.cards)
}

}

1 个答案:

答案 0 :(得分:0)

如果“ id”是唯一字段,则将标题映射到id并使用它进行过滤。

在component.ts文件中:

titleList: Array<string> = [];

ngOnInit() {
this.listofproducts=this.prservice.getallproducts()
Object.values(listofproducts).forEach(prod => {
this.titleList[prod['id]] = prod['title'];
});

现在让我们考虑一下屏幕截图中的数据。

{title: bread,.... id:'32'}
{title: bread123,.... id:'38'}

代码运行后在console.log(titleList)时,您将获得如下内容:

[ '32' : 'bread',
  '38' : 'bread123' ]

因此,现在您可以在html / ts中访问/过滤具有ID的标题。