Using ES6 Classes as Angular 1.x directives

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

    I faced the same problem. First time I tried to solve problem via ES6 classes but I have problem with $inject my dependencies. After I realized what angular have couple styles of writing code and I tried. At all I used John Papa styles and I got this works code in my rails app with ES6:

    ((angular) => {
     'use strict';
    
      var Flash = ($timeout) => {
       return {
         restrict: 'E',
         scope: {
           messages: '=messages'
         },
         template: (() => {
           return "
    " + "
    ×
    " + "{{ message[1] }}" + ""; }), link: (scope) => { scope.closeMessage = (index) => { scope.messages.splice(index, 1) }; $timeout(() => { scope.messages = [] }, 5000); } } }; Flash.$inject = ['$timeout']; angular.module('Application').directive('ngFlash', Flash); })(window.angular);

    I know that I can do little bit improvements with functions and variables in more ES6 style. I hope it helps.

提交回复
热议问题