Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在angular 10中使用异步管道检索数据后执行某些操作_Angular_Asynchronous_Rxjs_Observable - Fatal编程技术网

如何在angular 10中使用异步管道检索数据后执行某些操作

如何在angular 10中使用异步管道检索数据后执行某些操作,angular,asynchronous,rxjs,observable,Angular,Asynchronous,Rxjs,Observable,我使用异步管道绑定数据,如下所示:Angular 10 app.component.html: <table> <thead> <tr> <th>ID</th> <th>Name</th> </tr> </thead> <tbody *ngFor="let custome

我使用异步管道绑定数据,如下所示:Angular 10

app.component.html:

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody *ngFor="let customer of customers | async">
        <tr>
            <td>{{customer.id}}</td>
            <td>{{customer.name}}</td>
        </tr>
    </tbody>
</table>
app.component.ts:

constructor(private customersService: CustomersService) { }
    customers:Observable<any>;
    ngOnInit() {
        this.customers = this.customersService.getCustomers();
      }
这里我调用getCustomers方法,它通过 http GET方法并分配给可观察的客户

它工作正常。我想在从api检索数据后执行一些操作

那么如何使用异步管道实现这一点呢?

就像订阅一样,防止内存泄漏。要执行任何操作,我们可以使用map或filter来操作数据

你可以像下面这样做

 ngOnInit() {
     this.customers = this.customersService.getCustomers().map(resp => {
          // do the actions you want
     });
 }
快乐编码..:

与订阅类似,可防止内存泄漏。要执行任何操作,我们可以使用map或filter来操作数据

你可以像下面这样做

 ngOnInit() {
     this.customers = this.customersService.getCustomers().map(resp => {
          // do the actions you want
     });
 }
快乐编码..:

您可以通过管道将操作符导入源以执行一些副作用

恩戈尼特{ this.customers=this.customersService.getCustomers.pipe tapres=>{ //用“res”做点什么` } ; } tap内的操作将针对来自可观察对象的每个通知执行,并且不会影响源通知

工作示例:

您可以通过管道将操作符导入源以执行一些副作用

恩戈尼特{ this.customers=this.customersService.getCustomers.pipe tapres=>{ //用“res”做点什么` } ; } tap内的操作将针对来自可观察对象的每个通知执行,并且不会影响源通知


工作示例:

tap操作员的确切用途是什么?我可以使用map吗?map与tap类似,只是它用于返回转换后的值。将点击视为不返回的地图。因此,tap中的任何操作都不会对发出的值产生任何影响。贴图通常用于变换发射的值。例如,你收到一个对象,你希望从中提取并只使用一个值。@chirag:我在答案中添加了一个工作示例。tap运算符的确切用途是什么?我可以使用map吗?map与tap类似,只是它用于返回转换后的值。将点击视为不返回的地图。因此,tap中的任何操作都不会对发出的值产生任何影响。贴图通常用于变换发射的值。例如,你收到一个对象,你希望从中提取并只使用一个值。@chirag:我在答案中添加了一个有效的例子。我认为is-map是旧语法,它在角度10中不起作用不,它不是,它在我的angular 10 app=中工作得非常好,尽管您可以像Michael D this.customers=this.CustomerService.getCustomers.pipemapres=>{return{sumthing:res.id};//这是一个示例};我认为is-map是旧语法,它在angular 10中不起作用不,它在angular 10应用程序中工作得很好=尽管你可以像Michael D this.customers=this.CustomerService.getCustomers.pipemapres=>{return{sumthing:res.id};//这是一个例子};