0

I've run into a similar problem to Passing variable to directive template without creating new scope and this doesn't seem to help me. The code looks like;

.directive('billDir', function () {
    return {
        template: '<div><div draggable>hello {{msg}}</div></div>',
        link: function (scope, elem, attrs) {
            // scope.status = attrs.bill.getStatus();
            scope.msg = "world!";
        }
    }
})
.directive('draggable', function ($document) {
    return {
        scope: {
            bill: '='
        },
        replace: true,
        restrict: 'A',
        link: function (scope, element, attr) {
            // do something here
            var startX = 0,
                startY = 0,
                x = 0,
                y = 0,
                sourceX = 0,
                sourceY = 0,
                windowWidth = 0;

            element.on('mousedown', function (event) {
                event.preventDefault();
                startX = event.pageX - x;
                startY = event.pageY - y;
                $document.on('mousemove', mousemove);
                $document.on('mouseup', mouseup);
                windowWidth = $(window).width();
                sourceY = event.pageY;
                sourceX = event.pageX;
            }); // some more stuff, to the end of the directive

And billDir doesn't work as expected, I don't get anything in place of {{msg}}

What am I doing wrong here?

Fiddle

6
  • hmm... Could you create a simple fiddle/plunker please? Commented Mar 11, 2014 at 15:02
  • @glepretre added edit with link to the fiddle Commented Mar 11, 2014 at 15:16
  • Fiddle does work for me Commented Mar 11, 2014 at 15:19
  • 1
    @maurycy for me, its a blank result jsfiddle.net/asheshambasta/4b3NL/1/embedded/result (nothing unusual on the console either) Commented Mar 11, 2014 at 15:22
  • Screenshot: cl.ly/image/0A2F0M2K1Y1r Commented Mar 11, 2014 at 15:26

1 Answer 1

1

Looking deeper into your code, I found an understandable mistake with your directive markup. You must add restrict: 'E', when creating an HTML element through a directive.

app.directive("billInfo", function() {
    return {
        restrict: 'E',
        replace: true,
        template: '<div><div draggable>hello {{msg}}</div></div>',
        link: function (scope, element, attrs) {
            scope.msg = "world! It Works!";
        }
    }
});

If you do not declare a restrict: then angular assumes you want the directive to behave as an attribute to a div such as <div bill-info></div>

Here is the fiddle example: http://jsfiddle.net/bebold/4b3NL/9/

Sign up to request clarification or add additional context in comments.

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.