Problem Statement
I have below two parent and child data structures like this where on parent div header click dynamic directive for showing corresponding child records are created.
But when I click on parent header link corresponding children items are not showing in table structure.Can anyone see where it is wrong?
<script>
var app = angular.module('TestApp', []);
app.controller('TestCtrl', function ($scope) {
$scope.parents = [
{ parent_id: '1', name: 'Parent 1', count: '2' },
{ parent_id: '2', name: 'Parent 2', count: '3' },
{ parent_id: '3', name: 'Parent 3', count: '1' }
];
$scope.getchild= function (parent) {
var children_list = [
{ id: '1', parent_id: '1', name: 'Test Child 1' },
{ id: '2', parent_id: '1', name: 'Test Child 2' },
{ id: '3', parent_id: '2', name: 'Test Child 3' },
{ id: '4', parent_id: '2', name: 'Test Child 4' },
{ id: '5', parent_id: '2', name: 'Test Child 5' },
{ id: '6', parent_id: '3', name: 'Test Child 6' }
];
var directive_name = 'clschild_' + parent.parent_id;
app.directive(directive_name, function() {
var child_html = '<div class="panel-body"> \
<table ng-attr-id="tblchild_'+ parent.parent_id + '" class="table table-hover"> \
<thead> \
<tr> \
<td>Name</td> \
</tr> \
</thead> \
<tbody> \
<tr ng-repeat="child in children"> \
<td>{{child.name}}</td> \
</tr> \
</tbody> \
</table> \
</div>';
return {
template: child_html,
link: function (scope) {
scope.$apply(function () { scope.children = children_list; });
}
}
});
};
});
</script>
and corresponding HTML is
<div ng-app="TestApp" ng-controller="TestCtrl">
<div class="panel panel-default" ng-repeat="parent in parents">
<div class="panel-heading clearfix">
<a data-toggle="collapse" ng-href="#divchild_{{parent.id}}" ng-click="getchild(parent);" class="pull-left" style="padding-top: 7.5px;">{{parent.name}}</a>
</div>
<div ng-attr-id="divchild_{{parent.id}}" class="panel-collapse collapse">
<div class="clschild_{{parent.id}}">
<div class="panel-body">
aaa
</div>
</div>
</div>
</div>
</div>
and here is a plnkr example
I am learning angularjs day by day so please bear with me if I am wrong somewhere in implementing above.
Your help is appreciated.Thanks.
ng-repeatto show all the data.