Nick Heiner
Nick Heiner

Reputation: 122412

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?

Upvotes: 3

Views: 1176

Answers (1)

Mark Rajcok
Mark Rajcok

Reputation: 364677

$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)

    }
};    

Upvotes: 3

Related Questions