Urgent: I need a hackaround to stop template caching in $routeProvider

11,612 views
Skip to first unread message

Maximilian Eberl

unread,
Aug 2, 2013, 9:06:08 AM8/2/13
to ang...@googlegroups.com
AngularJS unfolds it's magic&power in combination
with server side logic.
I have an app that uses routing with ng-view.
I want to have .py (or .pl or .jsp) files as
templates.

It is IMHO one of the greatest design problems
of AngularJS that $routeProvider could not be
injected with a Service (aka is not dynamic
but static).

Neither dynamic URL nor non-caching is
possible. This is neanderthalian www!
The only alternative is to implement
a custom routing.

In further versions this should be made
configurable like:

App.config(['$routeProvider', function($routeProvider) {
  $routeProvider.cacheTemplates(false);
  $routeProvider.
    when('/',               {templateUrl:start.py}).
    ...
    otherwise({redirectTo: '/404'});
  }
]);

Has anyone found a usable solution for this???

Maximilian Eberl

unread,
Aug 2, 2013, 9:28:22 AM8/2/13
to ang...@googlegroups.com
Solved (more or less) - I found a dirty workaround:

App.run(['$rootScope','$templateCache',function($rootScope, $templateCache) {
   $rootScope.$on('$routeChangeStart', function() {
      $templateCache.removeAll();
   });
}]);

Found here:
http://stackoverflow.com/questions/14718826/angularjs-disable-partial-caching-on-dev-machine

Works in FF and Chrome - IE8 not yet tested.
But this is REALLY dirty.
Should be made configurable in a next version.

Ricardo Bin

unread,
Aug 2, 2013, 11:27:08 AM8/2/13
to ang...@googlegroups.com
Maybe using the event $routeChangeSuccess makes more sense... :)

Carles Serrano

unread,
Aug 2, 2013, 7:23:35 PM8/2/13
to ang...@googlegroups.com
Wouldn´t it be better to remove just the route´s cached template??

something like?

App.run(['$rootScope','$templateCache',function($rootScope, $templateCache) {
   $rootScope.$on('$routeChangeStart', function(event, next, current) {
      $templateCache.remove(current.templateUrl);
   });
}]);

Maximilian Eberl

unread,
Aug 3, 2013, 10:49:30 AM8/3/13
to ang...@googlegroups.com
The more I dive into AngularJS, the more I am
convinced that routing is the sick old man of Angular.
This is because of the extremely bad decision to
make routing static in a dynamicworld.

With the trick mentioned above I am able
to do "dynamic" routing by preventing
template Caching.

But now I found out that I cannot pass params
to the dynamic template files!

Passing params is a now-brainer for the
underlying $http function.

I hate to repeat myself: Angular unfolds it's
power&magic with dynamic server templates.
But the neanderthalian limitations of $routeConfig
are constantly throwing rocks in my way.

I understand that I have to replace routing
with a custom service.

PLEEEEEZZZ !!!
Rewrite routing from scratch to make it work
in a dynamic world. Deaf and dumb HTML
bricks are really yesterday's technology.

@Ricardo Bin
No. When the routing is already finished, the old
cached animal has already been loaded. Therefor
onRouteChangeStart.

@Carles Serrano
Why traverse the whole array in search of one item and remove it?
Unsetting the whole array is MUCH faster.
And as I use the whole routing dynamically, the whole cache is useless.

Pawel Kozlowski

unread,
Aug 3, 2013, 11:17:44 AM8/3/13
to ang...@googlegroups.com
This type of attitude that makes me sad. For me it is not how open
source works.... If I don't like something I change it and send a pull
request. If it is not accepted I'm forking the project or switching to
something else...

Just ranting and asking _someone else_ to rewrite (means - put effort)
something that _I_ don't like is very unfair IMO....

Good luck with your project...

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to angular+u...@googlegroups.com.
> To post to this group, send email to ang...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
AngularJS book:
http://www.packtpub.com/angularjs-web-application-development/book
Looking for bootstrap-based widget library for AngularJS?
http://angular-ui.github.com/bootstrap/

Martin Alix

unread,
Aug 3, 2013, 1:03:36 PM8/3/13
to ang...@googlegroups.com
Obviously someone agrees with you: you should take a look at ui-router at
https://github.com/angular-ui/ui-router

But they are called templates for a reason, and really should not change that much and that's why they are agressively cached.
Maybe you can read some more about angular's directives and services and/or review the way you have architected your app.
Your views should contain very little logic other than simple looping and ifs, they should use whatever scope their controllers give them and directives, both of which have services injected in them and these services can call whatever server side .py or .php you want through $http or $resources... Ui-router makes all that really easy!

Maybe if you show us some code, someone can chime in?

Maximilian Eberl

unread,
Aug 4, 2013, 10:39:13 AM8/4/13
to ang...@googlegroups.com
> https://github.com/angular-ui/ui-router

Thanks for the link, I am going to dive into it.


> Maybe you can read some more about angular's directives and services

I did and of course in services or directives You can play with $http as You like.
But I wanted to use the advantages of links (a href) and therefore tried routing.
Can I overwrite the routing functions with some custom stuff.


> Maybe if you show us some code, someone can chime in?

It's customer's property.
I have to strip of the customer specific parts.

Maximilian Eberl

unread,
Aug 4, 2013, 10:48:57 AM8/4/13
to ang...@googlegroups.com
@Pawel Kozlowski

> For me it is not how open source works.... If I don't like
> something I change it and send a pull request.

> Just ranting and asking _someone else_ to rewrite (means - put effort)

Didn't want to offend anyone.
I thought that this is a principal problem of AngularJS
and wanted to discuss it before changing anything.
In most apps ng-view is used for the main content
and not insignificant bricks of the app. And in a
dynamic world the content is dynamic.

Forking is not a solution, especially in such a central
functionality like routing.

Please don't feel upset!

Martin Alix

unread,
Aug 4, 2013, 12:10:22 PM8/4/13
to ang...@googlegroups.com
Ui-router is very flexible and will probably solve world hunger :)
For example you can provide a function as a template, and inject dependencies on a per route/state basis and use $routeParams... Also checkout the resolve: functionality...
Ui-router is being considered for inclusion in the core, IIRC

Daniel Tabuenca

unread,
Aug 4, 2013, 3:02:04 PM8/4/13
to ang...@googlegroups.com
I'm not sure using routing is what you want. The typical use of routing is to use statically defined template and combine that with usually JSON data from the server (loaded in either the "resolve" or during controller initialization.

What you are asking for, is to load an angular template from the server that is itself generated by another template technology. 

It seems the simplest solution to what you are trying to do is to not use routes at all, but just put a watch on $location.path(). This watch handler could simply keep a mapping of  fragment paths to server urls, find the right server url for the given  "route" and set it to a scope variable.  You then just use ng-include to have angular load that from the server. 

dev mondo

unread,
Aug 4, 2013, 3:06:06 PM8/4/13
to ang...@googlegroups.com
@Pawel
i am sure Maximilian did not mean to offend anyone, he was just describing something that many of us experienced with Routing.
yes we can pull request and angular team is really active on that. and we can fork, but you have to understand that many of us are not that great programmers or experts, and Routing as not a small part of angular.
also if i am not wrong i i think in one of angular videos the guys mentioned that they acknowledge the problem and the are working on it, and maybe integrate ui-router with angular.

@Maximilian 

as Martin suggested use ui-router it is so awesome and has features beyond the built in one, that you might not need now but you will certainty need it in the future. dive into it and you wil love it and never look back it is very stable too

Maximilian Eberl

unread,
Aug 5, 2013, 7:56:16 AM8/5/13
to ang...@googlegroups.com
> It seems the simplest solution to what you are trying to do is to
> not use routes at all, but just put a watch on $location.path().
> This watch handler could simply keep a mapping of  fragment
> paths to server urls, find the right server url for the given  "route"
> and set it to a scope variable.  You then just use ng-include to
> have angular load that from the server.

Yes, to play around with raw $location.path does the job, but
I am loosing the comfort of $routeProvider and many other
developers are used to invoke routing.

My idea was very, very simple: make caching configurable with
a boolean flag and proceed request params to those templates.
Maybe rethink the concept of a static root provider.
Not one Higgs Particle more.


Marcelo Dezem

unread,
Apr 7, 2014, 12:17:16 PM4/7/14
to ang...@googlegroups.com
Seems like Angularjs is not for your project. Its design aims to made easy single page web apps, fatty clients, etc. It expects that the client will do API calls to the server to get data, not templates (it's a contradiction use the word template, templates are static by definition), thus providing division between data and views. That's why you are getting frustrated by the routing system, you are developing on the neanderthalian www, where you do all the job on the server. For this cases there are out there a ton of tools, asp.net, jsp, etc. etc...

Good luck!
Reply all
Reply to author
Forward
0 new messages