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