通过从角度存储中的ID选择元素

时间:2019-05-19 15:56:39

标签: angular redux rxjs ngredux

我的商店中有以下数据:

{
 properties : 
  [
   {
    _id: 123.
    name: "Nice property"
   },
   {
    _id: 456.
    name: "Another nice property"
   }
 ]
}

在我的ngOnInit方法中,我想从商店中选择一个属性,将一个id像这样作为queryParam传递:

javascript
this.id = this.route.snapshot.paramMap.get('id');
this.property = this.ngRedux.select( state => state.properties).pipe(find( property => property._id === this.id))

显然,这行不通,但是它抓住了我正在尝试做的精神。

使用ngRedux通过id进行谷歌搜索几乎没有带来什么好处,所以我怀疑我这样做的方式是错误的。

我尝试使用.subscribe解决此问题,但是所有逻辑都必须在订阅回调中,这似乎是错误的,并且在我尝试创建反应式表单时不起作用。

1 个答案:

答案 0 :(得分:0)

尝试一下:

this.property = this.store.select((state) => state.properties)
    .pipe(map(properties => properties.find(x => x_.id == this.id)));

这应该适合您的给定方案。您只需从商店中选择属性,然后返回与您的ID匹配的第一个属性。

相关问题