angular - 访问接收到的数据的值
问题描述
如何访问我在控制台中收到的数据值?
我如何在这里访问两个数组元素的标题?我必须过滤列表 acc。到所有数组元素的标题,所以我需要访问元素的所有标题这是控制台的 ss
组件.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})
}
}
服务.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)
}
}
解决方案
如果 '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.xml 中使用 id 访问/过滤标题。
推荐阅读
- reactjs - React - Material UI 测试 TypeError:无法读取未定义的属性“获取”
- amazon-web-services - 如何在不使用托管 UI 的情况下使用 Postman 使用 AWS cognito 注册新用户
- c++ - 有没有办法调用对象的重写基类方法?
- ios - Xcode 12.4 中的模拟器很慢
- javascript - 需要帮助来跟踪更高或更低 HTML 的猜测
- javascript - Reactstrap 表头和表体不匹配
- openlayers - openweathermaps 天气图图层未在 openlayers 上正确显示
- php - Xdebug with VSCode for PHP CLI 不附加
- reactjs - 使用 react-csv 将数据导出为 csv
- ios - 在 SwiftUI 和 VoiceOver 中捕获两指双击?