I am a beginner Angular programmer, but I am really close to understanding the directives.
I create a fiddle here, but I have never used fiddle before, and it is not quite rendering ...
the tr-row is a directive. I am trying to loop through the data, and print a directive (row) per record. HTML:
<table ng-controller="fiddleCtrl">
<thead>
<th>id</th>
<th>name</th>
<th>description</th>
</thead>
<tbody>
<tr><tr-row ng-repeat="d in data" scdata="d"></tr-row></tr>
</tbody>
</table>
javascript:
var myapp = angular.module('myApp', [])
.controller('fiddleCtrl', ['$scope', function ($scope) {
$scope.data = [
{ id: 1, name: 'Fred', description: 'not the best worker' },
{ id: 2, name: 'Wilma', description: 'Freds Wife'},
{ id: 3, name: 'Barney', description: 'Freds best friend'},
{ id: 4, name: 'Louise', description: 'Never heard of Fred'},
{ id: 5, name: 'Tracy', description: 'Some Chick'},
{ id: 6, name: 'Foo', description: 'Inventer of bar'}
];
}]).directive('trRow', function ($compile) {
return {
restrict: "E",
replace: true,
link: function (scope, element, attrs) {
scope.id = scope.d.id;
scope.name = scope.d.name;
scope.desc = scope.d.description;
var tmpl = '<tr ><td>{{id}}</td><td><strong>{{name}}</strong></td><td>{{desc}}</td></tr>';
element.html(tmpl).show();
//var e =$compile(tmpl)(scope);
//element.replaceWith(e);
var e = $compile(element.contents())(scope);
},
scope: {
d: "="
}
};
});
should be easy. (le sigh)
any help would be appreciated, I REALLY need to understand this.
What is happening in my code is the tr-row directive has replace the table. I get a list of them, (with a tr INSIDE of a tr-row element but there is no table to display them in. I know this means I am close, but I cant think of any new combinations to try.
I just need a simple table with rows in it.
I appologise if this has been asked a million times, I seem to be unsure what to search for. I have tried so many things.