I'm using angularJS
and requirejs
via angularAMD
to wire together a complex application.
One of my states has two simple views like so:
$stateProvider
.state("common", {
url: "/",
views: {
"view1": {
templateUrl: "view1.tpl.html"
},
"view2": {
templateUrl: "view2.tpl.html"
}
}
});
html
<div ui-view="view1"></div>
<div ui-view="view2"></div>
View1
has a directive:
.directive('checkViewOneBoxWidth', function ($timeout) {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
var linkFunctionAfterDomComplete = function () {
console.log(elem[0].scrollWidth);
}
$timeout(linkFunctionAfterDomComplete, 0);
}
}
})
And view2
has a stylesheet that applies some styling to the page, including changing the height
and width
of the view1
container.
I want the function linkFunctionAfterDomComplete
to compute the element's height
and width
properties after the DOM has been modified by the stylesheet in view2
, so I wrapped the linkFunctionAfterDomComplete
function in a $timeout()
.
The console.log
from checkViewOneBoxWidth
here is reporting the size of this element, before the styling from the stylesheet in view2
modified the size of this element, despite the fact that we would expect the $timeout
to cause the linkFunctionAfterDomComplete
function to calculate the size of this element after styling from view2
is applied and the DOM is complete.
How can I get linkFunctionAfterDomComplete
to calculate element properties that reflect those which reflect the true state of the element after it had been fully rendered and all styling has been applied?