Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function after directive has been fully compiled / rendered

(Forgive me if I'm getting some of the terminology wrong; I'm new to Angular.)

I have a directive that looks something like this:

return {
    template: "<p>{{size}}</p><div ng-transclude></div>",
    link: function(scope, element) {
        scope.size = element.outerWidth();
    }
};    

I want this setting of scope.size to take effect after the ng-transclude has been resolved. However, when the link function is called, this is not the case. Is there some way to run code after ng-transclude subs in its content? Do I have to listen to load events in the DOM?

like image 571
Nick Heiner Avatar asked Dec 01 '25 18:12

Nick Heiner


1 Answers

$timeout() puts work on the native event queue, and hence it will be serviced after the browser renders. So try this:

return {
    transclude: true,
    template: "<p>{{size}}</p><div ng-transclude></div>",
    link: function(scope, element) {
        //scope.size = element.outerWidth();
        $timeout(function() {
            scope.size = element.outerWidth();
        },0)

    }
};    
like image 125
Mark Rajcok Avatar answered Dec 03 '25 10:12

Mark Rajcok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!