angular - How to push in array? -


i'm trying cart angular2(ts)

import {injectable} 'angular2/core';     import {cart} './product'; //interface id: number; name: string   @injectable()  export class productservice {  public products;  public cart : cart[] = [];      addtocart(products: object) {       console.log('product=',products)       this.cart.push(this.products);       console.log('cart=',this.cart);            } 

when push products in method

product= object {id: 13, name: "bombasto"} 

but in

console.log('cart=',this.cart); 

i have "undefined". why?

i think there typo in code:

addtocart(products: object) {   console.log('product=',products)   this.cart.push(products); // <--------   console.log('cart=',this.cart); } 

if want use products parameter, should remove this keyword when using @ level of push method.

with this keyword, use products property of productservice class undefined. try use undefined value in array... that's why see undefined in trace.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -