Currently, I am utilizing ES6 classes in order to build an angular controller. My goal is to use promises for loading my products. Upon calling getProducts, the data returns with the required information. However, there seems to be an issue as the this within this.products = results appears to be undefined resulting in a "Cannot set property 'products' of undefined" error. How can I effectively access properties from inside the 'then' function?
export class ProductController {
constructor (Product) {
'ngInject';
this.ProductService = Product;
this.products = [];
this.getProducts();
}
getProducts() {
let self = this; // Assigning 'this' to a variable that will maintain its reference
this.ProductService
.find()
.$promise
.then(function (results) {
self.products = results; // Accessing 'products' through the variable 'self'
}, function (results) {
console.log(results);
});
}
}