5

This is really weird.

Does not work

scope: { 'dataSource': '='}

Works

scope: { 'mySource': '='}

Works

scope: { 'data': '='}

Can anyone tell me why in the 1st fiddle console.log prints undefined, and in the second 99.

http://jsfiddle.net/xz261bam/

http://jsfiddle.net/mg9axkro/

1
  • 1
    its because of the keyword 'data'. 'xdataSource' would work just fine. Commented Aug 8, 2014 at 12:36

3 Answers 3

7

I think you will smack yourself on the head for this one... :)

The "data-" html5 prefix is stripped off, what you really get in your linking "source" not "dataSource":

angular.module('myApp', []).directive('myElement', function() {
    return {
        restrict: 'E',
        scope: {            
            'source': '=',            
        },
        template: '<p>test</p>',
        link: function(scope, iElement, iAttrs) {
            console.log(scope.source);
        }
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the "stripped off" and the actual linking evaluation :)
2

There is obviously a conflict with HTML data attribute if your name uses the data-whatever prefix. xdata-whatever or x-data-whatever is working just fine

<my-element xdata-source="counter"></my-element

or

<my-element x-data-source="counter"></my-element

example

Comments

1

Like everyone said, the data- prefix is stripped off, and change the scope's property name will solve the problem.

An alternative way is to double the data- prefix like this:

<my-element data-data-source="counter"></my-element>

JSFiddle: http://jsfiddle.net/4j3sks6u/

Comments

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.