Passing object to directive
Sending an object to a directive and let the directive render it with a template
HTML
<div ng-app="myApp">
<div ng-controller="myController">
<p>Components starts</p>
<div ng-repeat="item in items">
<!-- <p>{{item}}</p> -->
<my-component obj="item"></my-component>
</div>
<p>Components ends</p>
</div>
</div>
JavaScript
angular.module('myApp', [])
.controller('myController', function($scope) {
$scope.items = [
{ type: 'TextArea', data: 'Somedata1', mandatory: true },
{ type: 'List', data: 'Somedata2', mandatory: false },
{ type: 'Select', data: 'Somedata3', mandatory: true }];
})
.directive('myComponent', function() {
return {
restrict: 'E',
scope: {
obj: '='
},
template: '<div> This is the template for {{obj.type}}</div>'
};
});