title: Angularjs ng-include force reload
body: <p>I have read several posts on this topic, but am still unable to get data loaded via ng-include to re-load after initially being loaded from a remote data server.</p>
  
  <p>(Example <a href="https://stackoverflow.com/questions/14693815/how-to-reload-refresh-model-data-from-the-server-programmatically">How to reload / refresh model data from the server programmatically?</a> )</p>
  
  <p>I have a GSP page which renders a pull down menu of options, and when one is selected, ng-include calls a remote server and displays the data (in an ng-grid). Thus the ng-include needs the full path to the remote server in the URL. This code works correctly the <strong>first time</strong> an option is used, but will not re-load the data on subsequent selections. I understand that Angularjs is caching the data, but I need to force it to be re-loaded. Is there an event that I can catch inside "myTabController" to force it to call the ng-include? Is it possible to disable the cache? Since it is not possible to pass arguments from Angularjs into the URL of the ng-include, these parameters are being sent via AJAX calls in another section, so that the data server already has the parameters before the ng-include request is made.</p>
  
  <p>_myTabs.gsp</p>
  
  <pre><code>&lt;div ng-controller= "myTabController" ng-init="init(${selIds})"&gt;
  &lt;select name="tabSelect" ng-model= "tab" ng-options="t.name for t in tabs"&gt;&lt;/select&gt;
  &lt;form name="tabForm"&gt;
    &lt;div ng-switch="tab.value"&gt;
      &lt;div ng-switch-when="optionA"&lt;div ng-include="'https://datasrv:8453/DataApp/dataCtrl/aData'" &gt;&lt;div&gt;&lt;/div&gt;
      &lt;div ng-switch-when="optionB"&lt;div ng-include="'https://datasrv:8453/DataApp/dataCtrl/bData'" &gt;&lt;div&gt;&lt;/div&gt;
      &lt;div ng-switch-when="optionC"&lt;div ng-include="'https://datasrv:8453/DataApp/dataCtrl/cData'" &gt;&lt;div&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>myTabs.js</p>
  
  <pre><code>angular.module('myApp', ['ui.grid', 'ui.grid.resizeColumns', 'ngAnimate', 'ngRoute']);
  app.controller("myTabController", function($scope, $interval, $filter, $window, $http, $rootScope, uiGridConstants){
  $scope.tabs = {
    { name: '--- Please Select an Option ---', value: ' '},
    { name: 'A Label', value: 'optionA'},
    { name: 'B Label', value: 'optionB'},
    { name: 'C Label', value: 'optionC'}
   ];
  }]);
  </code></pre>
  
  <hr>
  
  <p>Edit:
  From the replies to my question it appears that calling $templateCache.removeAll() could solve this problem. I can add an onload="xx()" to the ng-include, and a function to myTabController:</p>
  
  <pre><code>$scope.xx = function(){
    $templateCache.removeAll();
  }
  </code></pre>
  
  <p>but of course it tells me that $templateCache is not defined. So I tried this inside of a run block:</p>
  
  <pre><code>$scope.xx = function(){
    app.run( function( $templateCache ){  
      $templateCache.removeAll();
    });
  }
  </code></pre>
  
  <p>This did not generate an error, but it didn't do anything. The function xx() was being called, but it was not calling $templateCache.removeAll(). What am I missing?</p>
  
  <hr>
  
  <p>Edit 2.</p>
  
  <p>I tried using a directive to generate the ng-include tag:</p>
  
  <pre><code>    &lt;div ng-switch-when="optionA"&lt;div mydir ng-attr-path="https://datasrv:8453/DataApp/dataCtrl/aData" &gt;&lt;div&gt;&lt;/div&gt;
  </code></pre>
  
  <p>In the directive, I tried adding a random number to the URL to make it unique:</p>
  
  <pre><code>.directive('mydir', ['$compile', function($compile){
    restrict: 'A',
    template: function( elem, attr ){
      var r = Math.random()
      var rpath = attr.path + '/?r=' + r
      return "&lt;div ng-include=\"'" + rpath + "'\" onload=\""xx('" + rpath + "')\"&gt;&lt;/div&gt;"; 
    }
  };
  </code></pre>
  
  <p>Unfortunately this did not make any difference. It did generate the ng-include correctly, including the random string at the end. It displayed the data once, but the URL was never re-generated on subsequent calls. The xx() function was called every time it was loaded, however.</p>
  
  <p>The key is to somehow force this directive to be called each time the menu item is selected, so the random number is called again to change the URL. Or use the xx() function to clear the cache. But how?</p>
  
  <hr>
  
  <p>Edit 3:
  I see that there are problems with passing values using the "{{val}}" notation into a directive into the URL for an ng-include. Reference: <a href="https://stackoverflow.com/questions/23413368/modifying-ng-include-directive">modifying ng-include directive</a></p>