Hi I'm new in AngularJS and have started to learn how to build directives
In my sample I'm trying to access to DOM elements that was rendered inside my directive, but I can't access some elements because they have not been rendered yet in my link function.
How I can access to those elements using ng-repeat and directives?
In my sample, I can get the "p" and change its color but I cannot access to td's for change its css properties, bind events, etc
HTML
<div ng-app="app">
<div ng-controller="myController as myCtrl">
<my-table list="myCtrl.list"></my-table>
</div>
</div>
JS
// Main module
(function() {
var app = angular.module("app", []);
}());
// Controller
(function() {
angular.module("app")
.controller("myController", myController);
function myController() {
var vm = this;
vm.list = [
{ id: 1, name: "Alan" },
{ id: 2, name: "Jasmine" }
];
}
}());
// Directive
(function() {
angular.module("app")
.directive("myTable", myTable);
function myTable() {
var directive = {
link: link,
replace: true,
restrict: "E",
scope: {
list: "="
},
template: "<div>" +
"<p>My Table</p>" +
"<table>" +
"<tr ng-repeat='item in list'>" +
"<td>{{item.id}}</td>" +
"<td>{{item.name}}</td>" +
"</tr>" +
"</table>" +
"</div>"
};
function link(scope, element, attrs) {
// "p" element is accesible and we can change the color
var p = element[0].querySelector("p");
angular.element(p).css("color", "red");
// CANNOT FIND TR'S, the element tag contains <!-- ngRepeat: item in list -->
var trs = element[0].querySelector("tr");
// ????????????????
}
return directive;
}
}());
https://jsfiddle.net/hkhvq1hn/
I aprecciate any help or suggestion Best