如何使用angular2或typescript将var中的数据设置为全局数据

时间:2017-11-30 06:53:23

标签: javascript angular typescript

我有一个功能,我在控制台中获取id但我无法在Apiservice中获取它,请任何人帮我做。

TS:

  deleteProduct(index,product) {
    var token = this.auth.getAccessTokenId();
    this.products.splice(index, 1);
    var del = this.products.splice(index, 1);
    del.map(function(product) {
    var Id = product.id;
    console.log(Id);
  })
    this.ApiService
     .deleteProducts(this.Id,token)
     .subscribe(
       products => {
         console.log(products);
         this.products = products;
       }, error => {
         //console.log(error);
       })
  } 

til console.log(Id);它工作正常,但它没有来到ApiService

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码段:

 deleteProduct(index) { 
var token = this.auth.getAccessTokenId(); 
var deletedProduct = this.products[index]; 
this.products.splice(index, 1); 
this.ApiService 
.deleteProducts(deletedProduct.id,token) 
.subscribe( 
products => { 
console.log(products); 
this.products = products; 
}, error => { 
//console.log(error); 
}) 
}