I'm trying to access DOM element in Directive's link function. The element is located in the view of another directive. Here is the code:
first directive
(function () {
'use strict';
angular.module("testAPP",[])
.directive('firstDirective', function(){
var directive = {
restrict: 'E',
templateUrl: 'firstDirective.html'
}
return directive;
})
})();
second directive
(function () {
'use strict';
angular.module("testAPP",[])
.directive('anotherDirective', function(){
var directive = {
restrict: 'E',
templateUrl: 'anotherDirective.html',
link: function($scope){
//element from another directive's view
var height = document.getElementByClassName("sky")[0].offsetHeight;
}
};
return directive;
});
})();
There is a console error saying that height variable is undefined. I tried timeout function and that worked for me, but i think it's not a good solution:
setTimeout(function(){
var height = document.getElementByClassName("sky")[0].offsetHeight;
console.log(height);
});
I also tried "require", but it caused an error that the directive can't be found (i think this might be because that directives are located in separate directories)
So, could you tell me the reason why require does not work, and suggest me better solution than timeout