Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have an $routeProvider in my AngularJS which provides a templateUrl and controller,

I was wondering if it is possible to make the temlateUrl within the controller instead.

Reason for this is my code is partially async and this would easen the complexity.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You need to have a template defined for a route (AFAIK), but you could have the template be:

<div ng-include="actualTemplateUrl"></div>

And in the controller set the URL:

$scope.actualTemplateUrl = "/path/to/actual/template.html";
share|improve this answer
    
can I not use the ng-view instead? – Scratch. Dec 19 '14 at 4:18
1  
ng-view populates it with the template defined at config-time with $routeProvider. Btw, you can have the templateUrl be a function, but if your requirement is that the controller decides on the template - then ng-include seems like the right way for me. EDIT: just to clarify - this would be done in addition to ng-view – New Dev Dec 19 '14 at 4:20
    
Sadly its not working for me.. when I try to change the $scope.templateUrl = "test.html"; inside the .then() field, it won't work, but outside it does. Async such a pain.. – Scratch. Dec 19 '14 at 4:41
    
works fine for me: plunker – New Dev Dec 19 '14 at 5:19
1  
$timeout is async and returns a .then-able promise. Go to plunker again to see the change - it works. Post your code to plunker if you're having other issues. For example, is your async code happens outside of the Angular digest loop? if you added $scope.$apply(), would that work? – New Dev Dec 19 '14 at 5:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.