AngularJS Directive using class

0

I need to create a directive but for the sake of organization I would like to do in a class, but it is giving an error that I can not identify. What I have is the following:

My class:

class MyDirective
  restrict: "E"
  replace: true
  scope:
    myVar: '='
  template: "<div>{{ myFunction(myVar) }}</div>"

  link: (scope, element, attrs, form)->
    scope.myFunction = (val)->
      //do something
      return 'my content'

My app:

myApp = angular.module('myApp', [ 'ngRoute', 'ngResource'])
myApp.directive "mydirective", MyDirective

I encounter the following error:

  

TypeError: Can not read property 'compile' of undefined

    
asked by anonymous 16.10.2014 / 14:33

1 answer

1

I found the answer, the directive method of angular expects a function, I can not actually make a class, but if I want to use a class I can do this:

class MyDirective
  constructor: ->
    return {
      restrict: "E"
      replace: true
      scope:
        myVar: '='
      template: "<div>{{ myFunction(myVar) }}</div>"
      link: (scope, element, attrs, form)->
        scope.myFunction = (val)->
          //do something
          return 'my content'
    }
    
16.10.2014 / 14:45