Using ES6 Classes as Angular 1.x directives

后端 未结 10 845
南旧
南旧 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:11

    I ran into this problem just now and I saw this topic. Tried some methods provided in discussion, I finally solved this problem in a very simple way:

    export default function archiveTreeDirective() {
        'ngInject';
    
        return {
            restrict: 'E',
            scope: {
                selectedNodes: "="
            },
            templateUrl: 'app/components/directives/archiveTree/archiveTree.html',
            controller: ArchiveTreeController,
            controllerAs: 'vm',
            bindToController: true
        };
    }
    
    class ArchiveTreeController {
        constructor() {
            'ngInject';
            ...
        }
        ...
    }
    

    I directly use function as the .directive('directiveName',factory) argument, and export it, later import it in module declaration. But I missed the "default" statement when exporting, so I got an error. After I add "default" key word, everything works!

    I find this method also works in my route configs (also in a function way).

    ============ Hope you can understand my poor English :)

提交回复
热议问题