4

I have a MVC .NET application with AngularJS. In my route provider I use the controllers of mvc for retreiving the views as follows:

   .when('/Units', {
        templateUrl: 'Unit/Units'
    })
    .when('/UnitsDetail', {
        templateUrl: 'Unit/UnitsDetail'
    })

And my .NET UnitController has the following methods:

 [Authorize]
    public ActionResult Units()
    {
        return View();
    }

    [Authorize]
    public ActionResult UnitsDetail()
    {
        ViewBag.reference = Guid.NewGuid().ToString().Substring(0, 6);
        return View();
    }

For the UnitsDetail view I need a reference that is generated in the UnitsDetail() method.

The problem comes when I go from Units to UnitsDetail several times. The first time the UnitsDetail() method is called and thus, the reference is generated but if I go back to Units and access again UnitsDetail the method is not called and the reference is the same. I need to generate one reference each time.

I know I could generate it using JS in the client or make an AJAX request from Angular to the server but what I really want to know is how to make Angular call the method UnitsDetail() every time I go to "#/UnitsDetail".

Thanks!

David Ortiz
  • 997
  • 1
  • 14
  • 22
  • It is just a wild guess, but can it be some agressive browser caching? – AndreySarafanov Nov 26 '15 at 09:39
  • Maybe Angular just generates the page once and the rest of the time just shows the first page. I tried in different browsers and they all do the same however I haven't found anything about how to prevent that from happening. – David Ortiz Nov 26 '15 at 09:41
  • seems like it has to be solved on the browser side http://stackoverflow.com/questions/23589843/disable-template-caching-in-angularjs-with-ui-router http://stackoverflow.com/questions/14718826/angularjs-disable-partial-caching-on-dev-machine – AndreySarafanov Nov 26 '15 at 09:50
  • I'm trying to keep using ng-route instead of ui-route and the solutions I've seen seem to just use ui-route for handling the cache. Is there any method with ng-route? thanks – David Ortiz Nov 26 '15 at 11:42
  • Sorry, I have a very shallow knowledge of Angular, so I unfortunately can't help you. – AndreySarafanov Nov 26 '15 at 12:10
  • Thank you anyway @AndreySarafanov ! – David Ortiz Nov 26 '15 at 12:56

1 Answers1

1

By default angular will cache all views in its template cache.

This is by design.

Angular expects the view to be just static HTML with the dynamic part marked using the {{ dynamicScopeVariable }} code. Angular will use scope objects to replaces the dynamic bit . But the static part will be shown from the cache.

The first time you execute the view , the view is cached.

You have 2 options here. Actually just one which is good from the Design point of you #2.

Option #1

Disable template caching in Angular as shown here https://stackoverflow.com/a/27432167/2794980

This means that angular will call the HTML ACTION every time it needs the view.

This is not the best way to use angualar but it will work. You should also consider the performance side .. By far the most time consuming call on a ASP.NET MVC applications are Actions calls. This method means that while you are using a client side MVC framework , you are not utilizing one of its important benifits.

Options #2

Use a angualar service to get the GUID from back end . i.e. the code below.

 ViewBag.reference = Guid.NewGuid().ToString().Substring(0, 6);

Preferably use a WebAPI if u can ( your request will be small ). The use angular to replace the GUID in ur VIEW.

It is not clear from the question why you need the GUID and if you could do with generating a random unique number using Javascript . If that is possible , it might be the best solution . But based on the info in the question you could use either of option 1 or 2.

Edit : Didn't realize that you have already tried option 2 , in that case Option 1 it is.

Edit 2 : If you want to remove a single element from the cache , you can do

 $templateCache.remove("<Element Name>");

or you could use the

$cacheFactory.remove("Name")

Read more about cache factory here : https://docs.angularjs.org/api/ng/service/$cacheFactory

The name is generally the page name , you can look at the template cache or cache factory object in debugger console to find the exact name.

Community
  • 1
  • 1
Pratik
  • 868
  • 1
  • 9
  • 24
  • Thanks @Pratik I'm currently using option #2. I've used the reference variable as an example, what I wanted to know was if it's possible to ensure that the Action method is called. That's why option #1 is useful, however I understand that this must be applied to the whole app. Is it possibe to disable Angular caching just for this route? or is it possible to erase the html of this view from the cache? Thanks again! – David Ortiz Nov 27 '15 at 20:44
  • 1
    @davidivad : updated the comment to show what you could do . – Pratik Nov 30 '15 at 10:49
  • Thank you so much for your help! I'll try to use the option #2 since is the most appropiate one, but is good to know the other ones. – David Ortiz Nov 30 '15 at 14:05
  • 1
    @davidivad happy to have helped :) – Pratik Nov 30 '15 at 14:40