1

I need to pass an array of photo description to my directive. As I am struggling to make it working, I just realized that angular took my list attribue as a string rather than an array.

Here is the directive call:

    <photos photoid="ImpactDemiPrecoce" list="[{file:'rsc/drive/4-Semis/d-ChoixDeLaDensite/ImpactDemiPrecoce'}]" extension="png"></photos>

The Directive is as follows:

app.directive("photos", function () {
    return {
        restrict: "E",
        replace: true,
        scope: {
            "photoid": "@",
            "scrollable": "@",
            "size": "@",
            "list": "@",
            "extension": "@"
        },
        template: 
        '<div id="photos{{photoid}}" class="scroller" ng-class="[{{scrollable}}]" ng-style="{width: list.length==1?size:\'auto\', height: size}">\n\
         # of photos is {{list.length}}.<br>\n\
            <div ng-repeat="p in list track by $index"\n\
                ng-style="{\'background-image\': \'url({{p.file}}.thumb.{{extension}})\'}"\n\
                ng-click="openPopoverImageViewer(\'#photos{{id}}\', {{$index}})">\n\
                <div>{{p.text}}</div>\n\
            </div>\n\
        <div>',
        link: function (scope, element, attr) {
            if (!scope.extension)   scope.extension="jpg";
            if (!scope.scrollable)  scope.scrollable="hScrollable";
            if (!scope.size)        scope.size="171px";
        }
    };
});

The directive displays "# of photos is 126." which is the length of the string within the list attribute.

How to do it?

1

1 Answer 1

1

You are setting the scope wrong for the list element. @ will take the value instead of the object. You need to use = instead.

app.directive("photos", function () {
    return {
        restrict: "E",
        replace: true,
        scope: {
            "photoid": "@",
            "scrollable": "@",
            "size": "@",
            "list": "=",
            "extension": "@"
        },
        template: 
        '<div id="photos{{photoid}}" class="scroller" ng-class="[{{scrollable}}]" ng-style="{width: list.length==1?size:\'auto\', height: size}">\n\
         # of photos is {{list.length}}.<br>\n\
            <div ng-repeat="p in list track by $index"\n\
                ng-style="{\'background-image\': \'url({{p.file}}.thumb.{{extension}})\'}"\n\
                ng-click="openPopoverImageViewer(\'#photos{{id}}\', {{$index}})">\n\
                <div>{{p.text}}</div>\n\
            </div>\n\
        <div>',
        link: function (scope, element, attr) {
            if (!scope.extension)   scope.extension="jpg";
            if (!scope.scrollable)  scope.scrollable="hScrollable";
            if (!scope.size)        scope.size="171px";
        }
    };
});

You can find the difference of @ and = over here well explained

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.