Which event fires when view is rendered in AngularJS?

499 views Asked by At

I know there's $viewContentLoaded, but it fires before AngularJS substitutes the placeholders with scope variables.

I managed to solve this by setting a timeout for 0 ms in my $viewContentLoaded listener, but that's very ugly.

I'm using this to parse LessCSS stylesheets included in partials, but I have an URL prefix field which I need to substitute to the stylesheet URL before I pass it to LESS.

Here's my code (with my ugly hack):

var AccountController = function($scope, UserService) {
    var user = UserService.get();
    $scope.username = user.profile.displayName || user.contact;

    var bindLESSInit = function($scope, linkElementSelector) {
        $scope.$on('$viewContentLoaded', function() {
            var linkElement = document.querySelector(linkElementSelector);
            if (!linkElement) throw new Error('bindLESSInit: link element not found');

            console.log('link href before timeout: ', linkElement.href);
            // BAD: outputs "http://localhost:8282/%5B%5BstaticURLPrefix%5D%5D/static/portal/app/less/account.less"
            setTimeout(function() {
                console.log('link href after timeout: ', linkElement.href);
                // GOOD: outputs "http://localhost:8282/static/portal/app/less/account.less"
                // clear previous view's styles
                less.sheets = less.sheets.filter(function(e) { 
                    return e.getAttribute('class') && e.getAttribute('class').match('view-style');
                });
                less.sheets.push(linkElement);
                less.refresh();   
            }, 0);

        });
    };

    bindLESSInit($scope, '#account-stylesheet');    
};

[...]

There's a related question here: How can I trigger an event when an angular JS route template has been loaded

I tried the answer, using $routeChangeSuccess instead, but it gives the same results.

Cheers

2

There are 2 answers

0
Jess On BEST ANSWER

This is a very long thread, but it might help you. From a quick read, I think this is not an angular way to do things. Instead it is suggested to use a directive to solve the problem.

https://github.com/angular/angular.js/issues/734

1
sevcsik On

Based on the answer of Jess, I took another path and solved it using a directive.

/**
 * Directive to load LESS stylesheets when inserted.
 * @param {attribute} url url of the stylesheet
 */
var lessStylesheetDirective = function() {
    var link = function(scope, element, attrs) {        
        var linkElement = $('<link rel="stylesheet/less" type="text/css">');

        // when link is called, we don't have the attribute yet, if it's interpolated.
        // see http://stackoverflow.com/questions/11913841
        attrs.$observe('url', function(value) {
            if (!value) return;

            if (!window.less) throw new error('LESS global not found!');  

            linkElement.href = value;
            less.sheets.push(linkElement);

            // we reload everything FIXME: can we reload only this one?
            less.refresh(); 
        });

        element.on('$destroy', function() {
            // we remove our style from less, so it won't be parsed again
            less.sheets = less.sheets.filter(function(e) { 
                return e !== linkElement;
            });
        });
    };

    return {
        link: link,
    }
};

app.directive('lessStylesheet', lessStylesheetDirective);

Usage:

<div less-stylesheet url="{{yourUrlComesHere}}"></div>