javascript - AngularJS controller inheritance in ES6 -


i've got project in angularjs , have basecontroller , child controller inheriting it.

class basecontroller {     constructor($log, $state) {         'nginject';          this.$log = $log;         this.$state = $state;     } }  class childcontroller extends basecontroller {      constructor(myservice) {         'nginject';          super();         this.myservice = myservice;     } } 

my question is: need inject parent dependency injections child controller when not using it?

above example shows want achieve, it's not working. got idea if can achieve without passing basecontroller services super($scope, $state) invocation?

the $injector service injects dependencies constructor function of instantiated class. not inject construction functions of ancestor classes.

to construct controller injectables ancestor class, use $injector service , angular.extend:

class basecontroller {     constructor($log, $window) {         'nginject';          this.$log = $log;         this.$window = $window;     } }  class childcontroller  ̶e̶x̶t̶e̶n̶d̶s̶ ̶b̶a̶s̶e̶c̶o̶n̶t̶r̶o̶l̶l̶e̶r̶ {      constructor(myservice, $injector) {         'nginject';         var base = $injector.instantiate(basecontroller);         angular.extend(this, base);         ̶s̶u̶p̶e̶r̶(̶)̶;̶         this.myservice = myservice;     }     $oninit() {       this.$log.info("$oninit");       this.$log.log(this.base);     } } 

the demo

class basecontroller {      constructor($log, $window) {          'nginject';            this.$log = $log;          this.$window = $window;          this.base = "hello basecontroller";      }  }    class childcontroller {        constructor(myservice, $injector) {          'nginject';          var base = $injector.instantiate(basecontroller);          angular.extend(this, base);          //super();          this.myservice = myservice;      }      $oninit() {        this.$log.info("$oninit");        this.$log.log(this.base);      }  }    angular.module("app",[])  .controller("child", childcontroller)  .value("myservice", {})
<script src="//unpkg.com/angular/angular.js"></script>    <body ng-app="app" ng-controller="child vm">      {{vm.base}}    </body>


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -