简体   繁体   English

地图如何使角度可观察

[英]How could a map make an observable in angular

I want to make a map to an observable in angular to obtain a property of the object (array) that I am consuming from an api, since I want to make a validation in that array to know if to update / create record. 我想在角度上建立一个可观察的映射,以获取我从api消耗的对象(数组)的属性,因为我想在该数组中进行验证以了解是否更新/创建记录。

Good afternoon, I wanted to make a query, I make http queries through observable, as I show below: 下午好,我想进行查询,通过可观察的方式进行http查询,如下所示:

public getAllEquipos() : Observable<Equipos[]> {
      return this.http.get<Equipos[]>(this.baseurl + 'equipos')
  }

And then I use this data in a data table of angular material: 然后,我在角度材质的数据表中使用此数据:

RenderDataTable() {   

  this.service.getAllEquipos().subscribe(  
    (res) => {  
      this.dataSource = new MatTableDataSource();  
      this.dataSource.data = res;  
    //  console.log(this.dataSource.data);
    //  this.dataSource.sort = this.sort; 
      this.dataSource.paginator = this.paginator;
       console.log(res)      
    },  
    (error) => {  
      console.log('Se produjo un error mientras intentaba recuperar Usuarios!' + error);  
    });

}

a question I was going to ask you, is it possible the variable res, which is the first parameter of the subscribe method, will you use outside of that function? 我要问的一个问题,是否可能是变量res,这是订阅方法的第一个参数,您会在该函数之外使用吗? Since it is the arrangement in question, I mention it to you because I need to make a map to the observable that I am getting to only obtain a property of the object, that is, I just want to bring the property to me, and then use this object or array to do a check. 由于这是有问题的安排,因此我向您提及它是因为我需要对可观察对象进行映射,使我只能获取对象的属性,也就是说,我只想将该属性带给我,并且然后使用此对象或数组进行检查。 when I want to update / create a record, since depending on whether the ID is already in the array the record will be updated, otherwise it will be created. 当我要更新/创建记录时,由于ID是否已在数组中,因此将更新记录,否则将创建记录。

Neither do I know how to apply the map to my observable, if you could help me with that I would greatly appreciate it. 我也不知道如何将地图应用于我的可观察对象,如果您能帮助我,我将不胜感激。

` Observable <Equipos[]>: in this case <Equipos[]> is an interface ` ` Observable <Equipos[]>: in this case <Equipos[]> is an interface `

PD: the getAllEquipos () function is in a service file while the RenderDataTable () function is in component PD:getAllEquipos()函数在服务文件中,而RenderDataTable()函数在组件中

you can apply an rxjs map operator in either the getAllEquipos() method of your service, or inside the RenderDataTable() method on your component. 您可以在服务的getAllEquipos()方法中或组件的RenderDataTable()方法中应用rxjs map运算符。 In either case, you will need the pipe and map operators. 无论哪种情况,都需要使用pipemap运算符。

map will automatically wrap the value returned from the transform function in an observable so that it continues to return an observable for either additional chaining or direct subscription. map会自动将从transform函数返回的值包装到一个observable中,以便它继续返回observable进行附加链接或直接预订。

import {pipe} from 'rxjs'
import {map} from 'rxjs/operators'

//at the service
public getAllEquipos() : Observable<Equipos[]> {
      return this.http.get<Equipos[]>(this.baseurl + 'equipos')
                                    .pipe(
                                          map(res => res.targetProperty)                                                                    

  }

//in your component
RenderDataTable() {   

  this.service.getAllEquipos().pipe(map(res => res.targetProperty)).subscribe(  
    (res) => {
      this.dataSource = new MatTableDataSource();  
      this.dataSource.data = res;  
    //  console.log(this.dataSource.data);
    //  this.dataSource.sort = this.sort; 
      this.dataSource.paginator = this.paginator;
       console.log(res)      
    },  
    (error) => {  
      console.log('Se produjo un error mientras intentaba recuperar Usuarios!' + error);  
    });

}


Since I don't know what your create/update code looks like, I'll do this as a function that returns true if the item already exists. 由于我不知道您的创建/更新代码是什么样子,因此我将其作为返回true如果该项目已经存在)的函数来true I'll assume that your Equipos class contains a numeric id property that uniquely identifies each item. 我假设您的Equipos类包含一个数字id属性,该属性唯一标识每个项目。

@Component()
class TableComponent {

    private existingIds: number[] = []; // initialize with empty array to avoid errors in itemExists

    RenderDataTable() {
        this.service.getAllEquipos().subscribe(  
            (res) => {
                this.dataSource = new MatTableDataSource();  
                this.dataSource.data = res;  
                //  console.log(this.dataSource.data);
                //  this.dataSource.sort = this.sort; 
                this.dataSource.paginator = this.paginator;
                console.log(res);

                // res is an array, so use map function to extract an array of id properties
                this.existingIds = res.map(item => item.id);
            },  
            (error) => {  
                console.log('Se produjo un error mientras intentaba recuperar Usuarios!' + error);  
            });
    }

    private itemExists(id: number) {
        return this.existingIds.includes(id);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM