I am new to this angular thing (and web-dev in general), but i have encountered a weird behavior concerning how the angular link function of a custom directive changes the DOM. More specifically the directive (using ng-repeat) creates multiple instances of a template: these instances are somehow accessible by document.getElementsByClassName() but not inside angular or jquery.
My index.html and group-widgets.html (containing template) are likewise:
<div class="groups_panel">
<group-widgets class="col-md-12" style="margin-top:60px;"></group-widgets>
</div>
<div class="col-md-6 comment" ng-repeat="group in gWidget.groups">
<div ...</div>
</div>
my app.js is like:
(function(){
var app = angular.module('pm',[]);
app.directive('groupWidgets',function(){
return{
restrict: 'E',
templateUrl: 'group-widgets.html',
controller:function($scope){
this.groups = allGroups;
console.log($scope);
console.log("Wait till renders...");
},
controllerAs: 'gWidget',
link: function(scope, element, attributes, parentCtrl){
console.log("LINKED!");
console.log(document.getElementsByClassName("comment"));
console.log(element.html());
console.log($(".comment"));
console.log("END");
}
};
});
})();
The allGroups variable is an array whose elements ng-repeat instantiate. After running it with chrome dev tools I get: Screenshot (Check inside LINKED! and END).
It is obvious that during the linking function the DOM created by the ng-repeat (four instances as show in screenshot) is available with getElementsByClassName() but not within angular element object neither within the entire DOM using jQuery!
I have solved the problem as described in other questions (Calling a function when ng-repeat has finished.,AngularJS: Linking to elements in a directive that uses ng-repeat), but I cannot comprehend the order that directive link function works and why this is happening. Obviously I am making something wrong. Can you please help me clarify the issue?
Thanks in advance.