Hello guys, I have the code down below and it works. Problem is I don’t understand what’s going on.
export class ListPage {
selectedItem: any;
icons: string[];
items: Array<any>;
Product(name, numberInStock, wantedNumber, presentation, productType: String[], brands?: String[]) {
this.name = name;
this.numberInStock = numberInStock;
this.wantedNumber = wantedNumber;
this.presentation = presentation;
this.productType = productType;
this.brands = brands;
}
constructor(public navCtrl: NavController, public navParams: NavParams) {
// If we navigated to this page, we will have an item available as a nav param
this.selectedItem = navParams.get('item');
this.items = [];
for (let i = 1; i < 11; i++) {
this.items.push(new this.Product("Sandwich Bread", 0, 1, "Bag", ["Breads"]));
this.items.push(new this.Product("Milk", 1, 2, "Carton", ["Dairy", "Drinks"]));
this.items.push(new this.Product("Bananas", 1, 1, "Hand", ["Breads"]));
}
}
itemTapped(event, item) {
// That's right, we're pushing to ourselves!
this.navCtrl.push(ListPage, {
item: item
});
}
}
-
Vscode says this.name, this.numberInStock, etc does not exist in list page but the code still works (I take the IDE takes it as an undeclared global variable while it actually behaves as an attribute of Product?). Which one is it?
-
If I declare the variables the error goes away (yay), but when I print them they are undefined (nay?)
export class ListPage {
selectedItem: any;
icons: string[];
items: Array<any>;
name: String; //Here
Product(name, numberInStock, wantedNumber, presentation, productType: String[], brands?: String[]) {
this.name = name;
this.numberInStock = numberInStock;
this.wantedNumber = wantedNumber;
this.presentation = presentation;
this.productType = productType;
this.brands = brands;
}
...
for (let i = 1; i < 11; i++) {
this.items.push(new this.Product("Sandwich Bread", 0, 1, "Bag", ["Breads"]));
this.items.push(new this.Product("Milk", 1, 2, "Carton", ["Dairy", "Drinks"]));
this.items.push(new this.Product("Bananas", 1, 1, "Hand", ["Breads"]));
}
console.log(this.name); //undefined
}
- I think its pretty obvious what I want to do here but could you point me out on the best way to do it? Maybe a provider that receives the data, creates and return the product? Thanks.