Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  1] [ 2]  / answers: 1 / hits: 18436  / 11 Years ago, mon, september 23, 2013, 12:00:00

i've a array. Every item of the array holds data for an directive. The array is created inside a controller.



The Model



$scope.myDirectiveData = 
[ { title: First title, content: First content },
{ title: Second title, content: Second content } ];


The Directive Template



<div class=MyDirective>
<h1>{{myDirectiveData.title}}</h1>
<p>{{myDirectiveData.content}}</p>
</div>


How should i implement the the directive to create a item for any item in the array ? Something like...



<MyDirective data-ng-repeat=item in myDirectiveData></MyDirective>

More From » angularjs

 Answers
41

Here is an example using a directive. It used ng-repeat to call your directive for each object in the array on your scope. In this fiddle is this what you are looking for.



http://jsfiddle.net/gLRxc/4/



angular.module('test', [])

.directive('myDirective', function () {
var template = '<div class=MyDirective>' +
'<h1>{{ data.title }}</h1>' +
'<p>{{ data.content }}</p>' +
'</div>';
return {
template: template,
scope: {
data: '='
},
link: function (scope, element, attrs) {

}

};
})

.controller('TestController', function ($scope) {

$scope.myDirectiveData = [
{ title: First title, content: First content },
{ title: Second title, content: Second content }
];


})
;


Html Edited: the ng-repeat is on the same line as the directive like your question states.



<div ng-app=test ng-controller=TestController>
<div my-directive data=item ng-repeat=item in myDirectiveData></div>
</div>

[#75497] Sunday, September 22, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
moriah

Total Points: 201
Total Questions: 100
Total Answers: 82

Location: Tuvalu
Member since Sun, Sep 4, 2022
2 Years ago
moriah questions
Mon, Aug 17, 20, 00:00, 4 Years ago
Tue, Nov 12, 19, 00:00, 5 Years ago
Wed, Aug 7, 19, 00:00, 5 Years ago
Tue, Jul 9, 19, 00:00, 5 Years ago
;