javascript - 订阅时Angular 2更改数组值

标签 javascript arrays angular typescript

我想在订阅后更改数组中的一些值。

this.data = this.search.getProductsById(this.id).subscribe
    ((data: any) => {
        this.list = data;
        //Here for each price I want to change price value 
        for (let entry of this.list.deals.new) {
            if (entry.country == 'UK') {
                let p = entry.price * 1.10837; //here I change price
                console.log(p); //displays new price
            }
        }
        this.loading = true;
    });

但在 HTML 中它显示旧价格,而不是 p。如何更改它以便进入 html 新的?

最佳答案

我认为这是因为你没有在你的数组中设置新值p,我认为它是这样的:

this.data =  this.search.getProductsById(this.id).subscribe
((data: any) => {
    this.list = data;
//Here for each price I want to change price value 
   for(let entry of this.list.deals.new){
     if(entry.country == 'UK'){
      entry.price *= 1.10837;
      console.log(entry.price); //displays new price
     }
   }
    this.loading = true;
}

关于javascript - 订阅时Angular 2更改数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54023517/

相关文章:

javascript - dGrid 表格以编程方式水平滚动

javascript - Nodejs/JavaScript 模块加载和引用

c - 在循环内声明时数组基地址正在更改

不需要内存空间的 C++ 零大小数组?

Angular 5 : Is there way hide API call? 还是将其设为私有(private)?

javascript - jQuery Load() 未在 div 内加载

javascript - TypeScript:访问类中的静态方法(相同的或其他的)

php - 将 html 表转换为 php 数组的最快方法是什么?

angular - 在 Angular 中模拟 NgbModal

javascript - 如何在图像src ionic中使用变量