Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass an array into a directive without it turning into a string?

Righty, so I'm just getting into directives and they seem pretty awesome. I ran into a problem though:

I need to pass an array of images into a directive so I can filter them by certain criteria. Here's my html invoking the directive:

<img cover="{{challenge.images}}">

This is my directive:

myproject.directive('cover', function() {
    return {
        link: function ($scope, element, attrs) {
            console.debug("attrs.cover", Array(attrs.cover));
        }
    };
});

The output is a String. is there a way to prevent attr turning into a String?

like image 789
Dine Avatar asked Dec 03 '25 10:12

Dine


1 Answers

I'm assuming here that you don't want to create isolated scope, so:

myproject.directive('cover', function($parse) {
    return {
        link: function ($scope, element, attrs) {

            var covers = $parse(attrs.cover)($scope);
            console.debug("attrs.cover", covers);
        }
    };
});

and then use the directive like so:

<img cover="challenge.images">
like image 106
pkozlowski.opensource Avatar answered Dec 05 '25 00:12

pkozlowski.opensource



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!