角度2变化检测不起作用

时间:2017-06-14 22:54:15

标签: angular

我一直在尝试将我的产品保存到购物车中,但它似乎无法渲染。

我尝试过这些技术来检测变化

ChangeDetectorRef.detectChanges()

NgZone.run(callback) 

ChangeDetectionStrategy



export class CartPageComponent implements OnInit{

  cart: any;
  ids: any;

  constructor(private storage: LocalStorageService, private Product: ProductService ){
    this.cart = [];
    this.ids = [];
  }

  ngOnInit(){
    if(!this.storage.get('cart')){
      this.cart = this.storage.get('cart');
      return;
    }
    this.cart = JSON.parse(this.storage.get('cart'));
    this.ids = this.cart.map(function(item){
      return item.id;
    });
    
    // make http request and response will get back the data from the observable
    this.Product.getCartProducts(this.ids, function(response){
        this.cart = response.data;
    }.bind(this));

  }
}




如果在加载组件并且HTTP请求仍在运行之后,任何人都有任何更新购物车的线索,这将是一个很好的开始。

0 个答案:

没有答案