0

I have no idea why, but:

@myApp.directive 'myDirective', () ->
return {
    restrict: 'E',
    scope: {
        source: '='
    },
    link: (scope, element, attrs) ->
        console.log scope.source
}

<my-directive source="foobar"></my-directive>

returns undefined. The thing which confuses me is that in my other directive, print-user, everything works fine.

return {
restrict: 'E',
scope: {
  user: '=',
  showName: '=',
  showAvatar: '=',
  avatarSize: '='
},
templateUrl: 'templates/partials/print-user.html',
link: (scope, element, attrs) ->
  scope.tooltip = scope.user.username
}

Here I'm able to get the user object within the template by {{user}}.

Within myDirective I could get the source attribute by attrs.source - but why is it working within my user directive?

EDIT / Solution Thanks to Aleksandar Bencun: using <my-directive source="'foobar'"></my-directive> (additional single quotes) solved the problem.

8
  • Is this pure JS? Because the console.log scope.source is not a valid syntax, it should be console.log(scope.source); Commented Jun 23, 2016 at 17:29
  • It's CoffeeScript. Commented Jun 23, 2016 at 17:41
  • Can you provide a JS Fiddle that demonstrates the issue with this exact same code? This code seems perfectly valid to me, something else in your code might be the culprit. Commented Jun 23, 2016 at 17:55
  • Is foobar a variable and is it defined ? With scope.source angular take it as a variable. Commented Jun 23, 2016 at 18:26
  • 1
    Then you should write it like this: source="'foobar'", note the single quotes inside the double quotes. Look at this question: stackoverflow.com/questions/21001198/… Commented Jun 23, 2016 at 21:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.