Can I pass a json object as a directive attribute ?

5,906 views
Skip to first unread message

Noobcodeur

unread,
Mar 21, 2013, 2:16:20 PM3/21/13
to ang...@googlegroups.com
Hi All,

Just got a qucik question,

I am creating a component and was wondering if I could pass an object as attribute. It is basically a list of url that I would like to set as local scope for a directive for a slideshow/carousel component I am creating.

Something looking like that :

<slideshow imurls="{{ {img1:"/img/ddfdf.png", img2:"/img/gryuy.png"} }}">
</slideshow>

Many thanks,

A.

Clint Checketts

unread,
Mar 21, 2013, 2:57:21 PM3/21/13
to ang...@googlegroups.com
You could manually $eval it: http://plnkr.co/edit/KHIGcT3CHReg4xVSaHgy?p=preview

app.directive('slideshow',function(){
  return {
    restrict: 'E',
    link: function(scope, elem, attr){
      var a = scope.$eval(attr.imurls);
      console.log('imurls',a);
    }
  };
});


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Noobcodeur

unread,
Mar 25, 2013, 8:36:26 AM3/25/13
to ang...@googlegroups.com
Thanks a lot.
The only problem is that I am not using the link function but the compile function as I am modifying the dom template. So I have no access to the scope. ( so no scope.$eval. unfortunately).
Do you reckon I need to parse the string passed as attribute myself ?

It's basically just to be able to extract the urls from the object with a "for..in" loop and modify the background of some div I will append back to "element".

Clint Checketts

unread,
Mar 25, 2013, 1:54:00 PM3/25/13
to ang...@googlegroups.com
I suspect you'd want to move this into the link function. But worst case you could inject the $rootScope into the directive and $eval against that


--

Sander Elias

unread,
Mar 26, 2013, 4:45:46 AM3/26/13
to ang...@googlegroups.com
Hi A,

If it is just an array off url's why don't you just put in as something like this:
<slideshow imurls="{{ "'/img/ddfdf.png','/img/gryuy.png'" }}">

and then simply do something like:
  images = attr.imurls.split(",")
you might need to do some extra string cleaning, but this way you don't need the link function.

Regards
Sander Elias

Noobcodeur

unread,
Mar 26, 2013, 2:58:02 PM3/26/13
to ang...@googlegroups.com
Hi All,
Many thanks for the help.

I ended up using a link function and injecting the compile service. So basically, I tell angular to compile each time I modify the template in the link function.
Well all is clear now except that my widget is still not working but that's probably something else. The debugger doesn't show me anything so might be a mere css problem (I hope).

Good thing is that by using the link function, I was dealing with an object from the begining and not strings (as happens while manipulating dom in compile function). Easier to extract data from.

So basically it is a lot of $compile(dom_or_html_or_jQueryobject)(scope);.

Thanks again for your help really appreciated.
Reply all
Reply to author
Forward
0 new messages