Skip to content

Instantly share code, notes, and snippets.

@tiddle
Created March 24, 2016 10:02
Show Gist options
  • Save tiddle/b6ba67e7a33d3350baa1 to your computer and use it in GitHub Desktop.
Save tiddle/b6ba67e7a33d3350baa1 to your computer and use it in GitHub Desktop.
es6 'this' issue
export default function ProductComponent() {
return {
template: '<product-list sort-function="vm.sort"></product-list>'
controller: ProductController,
controllerAs: 'vm'
}
}
class ProductController {
constructor(ProductService) {
this.ProductService = ProductService;
}
sort(sortType) {
// When executed from within ProductListComponent
// 'this' is the this from inside ProductListComponent
console.log(this, sortType);
// getPRoducts returns a promise
return this.ProductService.getProducts(sortType)
}
}
ProductController.$inject(['ProductService']);
export default function ProductListComponent() {
return {
scope: {
sortFunction: '&'
},
template: '<a href="#" ng-click="vm.sort(\'id\')">id</a>',
controller: ProductListController,
controllerAs: 'vm'
}
}
class ProductListController {
constructor() {
this.sort = this.sortFunction();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment