Angularjs Directive using class

Asked

Viewed 71 times

0

I need to create a directive but for the sake of organization I would like to do it in a class, but it’s making a mistake that I can’t identify. Here’s what I got:

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

find the following error:

Typeerror: Cannot read Property 'Compile' of Undefined

Inserted the function directly into the directive works, I’m doing something wrong or really the angular does not have this kind of support?

  • You can post the function generated in Javascript to MyDirective?

  • Thank you, I’ve found the answer

1 answer

1


Found the answer, the angular Directive method expects a function, I really can’t make a class, but if I want to use a class I can do so:

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'
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.