Passing object to directive
Sending an object to a directive and let the directive render it with a template
by Gabriel Barcia
HTML
<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>
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 {
restric: 'E',
scope: { obj: '=obj' },
template: '<div> This is the template for {{scope.obj.type}} </div>'
};
});