Created
March 24, 2016 10:02
-
-
Save tiddle/b6ba67e7a33d3350baa1 to your computer and use it in GitHub Desktop.
es6 'this' issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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