In this plunk I'm trying to obtain in a directive an array with values entered in a list of <li> within the directive declaration.
Given this directive declaration:
<directive>
<ul>
<li id="0">xxx 0</li>
<li id="1">xxx 1</li>
<li id="2">xxx 2</li>
</ul>
</directive>
The array should return something like this:
scope.array = [ {id:0, name: "xxx 0"}, {id:1, name: "xxx 1"}, {id:2, name: "xxx 2"} ];
This is my attempt (that doesn't work):
angular.module('app', []);
angular.module('app')
.directive('directive', function() {
var directive = {};
directive.restrict = 'AE';
directive.template = "<div>{{array}}</div>";
directive.scope = true;
directive.link = function(scope, element, attrs) {
scope.array = element.find("ul")[0].children;
};
return directive;
});
lielements. You'll need to go through each one and pull out the id attribute and the content, create the object, and push it to the array. I must say this is a strange thing to write a directive for, but hey, party on.