Passing an array through an attribute to the AngularJS directive

I am currently having a problem passing an array to a directive through an attribute of this directive. I can read it as a String, but I need it as an array, so this is what I came up with, but it does not work. Help someone? thks in advance

Javascript ::

app.directive('post', function($parse){ return { restrict: "E", scope:{ title: "@", author: "@", content: "@", cover: "@", date: "@" }, templateUrl: 'components/postComponent.html', link: function(scope, element, attrs){ scope.tags = $parse(attrs.tags) } } } 

HTML ::

 <post title="sample title" tags="['HTML5', 'AngularJS', 'Javascript']" ... > 
+49
arrays angularjs parsing attributes directive
Apr 30 '13 at 1:23
source share
2 answers

If you access this array from your area, that is, load it into the controller, you can simply pass the variable name:

Binding an array to a variable directive in AngularJS

Directive

 scope:{ title: "@", author: "@", content: "@", cover: "@", date: "@", tags: "=" }, 

Template:

 <post title="sample title" tags="arrayName" ... > 
+54
Apr 30 '13 at 2:26
source share

you can also use $ scope instead of attrs. then you get an array object, otherwise you get a string.

  scope:{ title: "@", author: "@", content: "@", cover: "@", date: "@", tags: "=" }, link: function(scope, element, attrs){ scope.tags = scope.tags } 
+2
May 28 '16 at 19:50
source share



All Articles