2017-04-17 64 views
0

我想在快速会话中使用多个过滤器,我正在获取数组中的先前数据以及新数据。如何删除以前的数据。在订阅之前重置数组angular2

component.ts

ngOnInit() { 
    this.vehicleAttribute = JSON.parse(sessionStorage.getItem('vehicleAttributes')); 
    const data: ProductCatalogPostData = { 
    vehicleModelAttributes: this.vehicleAttribute.vehicleModels[0].modelAttributes, 
    role: 'vehicle owner', 
    planType: this.planType 
    }; 
    const page: ProductCatalogPostPagination = { 
    page: this.pageNo 
    }; 
    this.productCatalogService.compatibleProducts(data, page.page).subscribe(
    response => { 
     for (let i = 0; i < response.products.length; i++) { 
     this.lists.push(response.products[i]); 
     } 
     this.pageCount = response.totalCount; 
    }, 
    error => { 
     console.log(error); 
    } 
); 
} 

的过滤功能我想提出尽量使列表为空

onSelectedChange($event) { 
    this.lists = []; 
    this.planType = $event[0] 
    this.ngOnInit(); 
} 

,但我还是以前的订阅数据与新的数据一起。任何输入的高度赞赏

+1

从过滤函数调用'ngOnInit'绝对不是好。你永远不应该这样称呼它。整个观点是Angular称之为。 –

+1

为什么你会期望不同的数据? 'productCatalogService.compatibleProducts'很可能会返回相同的结果,无论您是否没有任何参数化。 –

+0

@AluanHaddad on'selectedChange'函数我得到了我的'planType'值,所以我用新的planType值调用了http请求。所以这就是我打电话给'ngOnInit()'的原因(如果有其他方法,请指导),并且我的计划类型值变化会得到不同的数据。我没有任何帮助地使用了debounce函数 – Prasadau

回答

0

您可以使用做如下运营商实现

this.productCatalogService.compatibleProducts(data, page.page) 
    .do(data => { 
     if(isEmpty(data)) { 
      this.lists= new Array(); 
     } 
    }) 
    .subscribe(response => { 
     for (let i = 0; i < response.products.length; i++) { 
     this.lists.push(response.products[i]); 
     } 
     this.pageCount = response.totalCount; 
    },error => { 
     console.log(error); 
    }); 
+0

我得到的错误有'.do不是函数' – Prasadau

0

我不太明白为什么是你迭代结束了,为什么单单不

this.list = response.products 

但如果你有一个理由不这样做,那么你可以复制元素

this.list = [].concat(response.products) 

所有把他们一次全部,如果你因为某些原因不想清理

this.list.push(...response.products) 

最后一个将等于:

Array.prototype.push.apply(this.list, response.products) 
+0

我使用推送功能的原因是过滤器可以分页。 – Prasadau

+0

对不起,我不明白你在说什么 – Damask