0

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.

1 Answer 1

5

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";
Sign up to request clarification or add additional context in comments.

6 Comments

can I not use the ng-view instead?
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
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..
works fine for me: plunker
$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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.