This is more of a javascript question but it's in the context of angularjs specifically. I'm trying to prepare for our eventually (5+ years? yes I know) migration to Angular 2+. I'd like to create new apps that are similar to Angular 2+ in look and feel. I created a helper function named Component() which acts as the dectorator @Component() and I'd like to place that above a class that acts as the angularjs 1.5+ .component().
However, since my Component() function is executed first it complains that the class isn't defined yet. But if I just use a function for the controller it works just fun even though it wasn't defined yet. Just curious why classes need to be defined in this case first but functions don't?
Component() helper function:
function Component(config) {
angular.module(config.module)
.component(config.selector, {
templateUrl: config.templateUrl,
controller: config.controller
});
}
This works:
Component({
module: 'myApp',
selector: 'heroList',
templateUrl: 'Components/Hero/hero.template.html',
controller: HeroComponent
})
function HeroComponent() {
console.log("Yay!");
}
This doesn't but only because the class definition is second. If it's first it works but that obviously kills the look and feel to Angular 2+
Component({
module: 'myApp',
selector: 'heroList',
templateUrl: 'Components/Hero/hero.template.html',
controller: HeroComponent
})
class HeroComponent {
constructor() {
console.log("Inside hero component!");
}
$onInit() {
}
}