Using ES6 Classes as Angular 1.x directives

后端 未结 10 811
南旧
南旧 2020-12-04 07:57

I\'m doing a small project to play around the goody bag the ES6 is bringing, I\'m trying to set register a class as an angular directive, but I\'m running into this error \"

10条回答
  •  有刺的猬
    2020-12-04 08:24

    My solution:

    class myDirective {
       constructor( $timeout, $http ) {
           this.restrict = 'E';
           this.scope = {};
    
           this.$timeout = $timeout;
           this.$http = $http;
       }
       link() {
           console.log('link myDirective');
       }
       static create() {
           return new myDirective(...arguments);
       }
    }
    
    myDirective.create.$inject = ['$timeout', '$http'];
    
    export { myDirective }
    

    and in the main app file

    app.directive('myDirective', myDirective.create)
    

提交回复
热议问题