Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run ES6 constructor in Angular 1.5 component

I try to run the ES6 constructor code when the 'soran' tag is loaded. but it just ignores. how can I run certain code by using a controller in angular 1.5 component() ?

import { config } from './index.config';
import { routerConfig } from './index.route';
import { runBlock } from './index.run';
import { MainController } from './main/main.controller';

angular.module('soranSpace', ['ngAnimate', 'ngCookies', 'ngSanitize', 'ngMessages', 'ngAria', 'restangular', 'ui.router'])
  .config(config)
  .config(routerConfig)
  .run(runBlock)
  .controller('MainController', MainController)

  .component('soran', {
    bindings: {},
    template: '<div id="map-viewport"></div>',
    scope: {},
    controller: soranController
  });

class soranController {
  constructor($log) {
    'ngInject';

    $log.log("run!");  // how to run this ? 
  }
}
like image 822
David Hong Avatar asked Jul 20 '26 04:07

David Hong


1 Answers

Move the class definition above the component definition. In ES6, class declarations are not hoisted, so soranController is not defined at the time where your component is defined. I ran into this same issue last week, and it didn't throw any error for some reason - I reckon it's something to do with how the code gets transpiled.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Hoisting

like image 143
Joe Clay Avatar answered Jul 22 '26 02:07

Joe Clay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!