angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Initial route update doesn't happen if ngView in a template loaded by ngInclude

Open jswright opened this issue 14 years ago • 26 comments

I can't seem to reproduce this on jsFiddle (mhevery says it's because you need real async with delay to reproduce, but the only way I know to include templates for ngInclude in jsFiddle are inline script tags), but here's how to set it up with two local files:

index.html

<html>
  <head>
    <script src="http://docs.angularjs.org/angular-1.0.1.min.js"></script>
    <script type="text/javascript">
      angular.module('myApp', [], function($routeProvider) {
        $routeProvider.when('/topic', {
          controller: TopicsCtrl,
          template: 'Controller: {{name}}'
        });
        $routeProvider.when('/topic/:topicId', {
          controller: TopicCtrl,
          template: 'Controller: {{name}}<br />Topic ID: {{params.topicId}}'
        });
        $routeProvider.otherwise({redirectTo: '/topic'});
      });

      function TopicsCtrl($scope) {
        $scope.name = 'TopicsCtrl';
      };

      function TopicCtrl($scope) {
        $scope.name = 'TopicCtrl';
        $scope.params = $routeParams;
      };
    </script>
  </head>
  <body ng-app="myApp">
    <div ng-include src="'app.html'"></div>
  </body>
</html>

app.html

<a href="#/topic">All topics</a>
<a href="#/topic/1">Topic 1</a>
<a href="#/topic/2">Topic 2</a>
<a href="#/topic/3">Topic 3</a>
<hr />
<div ng-view></div>

Expected results:

Page loads, redirects to #/topic, and shows 'Controller: TopicCtrl' in the view.

Actual results:

Page loads, does not redirect. If you click any link, the proper view is loaded. However, if you then reload, the view will not be properly loaded.

If you move the ng-view into index.html it works fine.

jswright avatar Jul 31 '12 22:07 jswright

Miško writes:

if ng-view instantiation is delayed (through ng-include) then the $route instantiation is delayed as well. This means that $route will miss the location change event.

Fix: Run $route update function on instantiation (not just on the location change event)

jswright avatar Jul 31 '12 22:07 jswright

After a bunch of false starts, here's how I did it (using the proposed fix):

If this is my app definition:

var myApp =  angular.module( ... );

This is where I update the $route update function:

myApp.run(['$route', function($route)  {
  $route.reload();
}]);

Now ng-view can be reliably nested inside ng-include.

myApp.run() is the place for application initialization: http://docs.angularjs.org/api/angular.Module

saidimu avatar Oct 04 '12 18:10 saidimu

+1

Thanks for the fix.

fredrikbonander avatar Mar 12 '13 15:03 fredrikbonander

As part of our effort to clean out old issues, this issue is being automatically closed since it has been inactivite for over two months.

Please try the newest versions of Angular (1.0.8 and 1.2.0-rc.1), and if the issue persists, comment below so we can discuss it.

Thanks!

btford avatar Aug 24 '13 00:08 btford

This issue still exists, but saidimu's fix still works. You don't have to call $route.reload() though, including it as a dependency to a run function is enough. I wound up with this:

myApp.run(['$route', angular.noop]);

tobyn avatar Sep 06 '13 19:09 tobyn

This is still an issue in 1.2.0-rc.3

The workarounds mentioned above work but if you inject $route into a run block all unit tests depending on the main module and using $scope.apply() fails (the app is doing $http calls to get the template when the route reloads or something). Don't know if that is a separate issue or not but certainly related to the workaround.

cmkarlsson avatar Nov 05 '13 12:11 cmkarlsson

Bump..

this issue is still there, it is call controller twice when I inject $route to app.run. Why?

raitucarp avatar Feb 24 '14 17:02 raitucarp

does anyone know if this annoying bug will be fixed in 1.3 ?

morgan-germain avatar May 05 '14 08:05 morgan-germain

hello I am trying to load a template located on a seperate file, but when I look at the console, the template doesnt load up. Can someone help me here? Thanks

watat83 avatar Jul 02 '14 18:07 watat83

I just checked it locally, and this still happens in the latest snapshot. Reopen.

Narretz avatar Jul 02 '14 19:07 Narretz

I am also experiencing this issue (in 1.2.18), but haven't seen it in other projects where I'm using a different (I think older) version - is it fixed in some versions?

rozza2058 avatar Jul 05 '14 09:07 rozza2058

Had to add $route.reload() again with 1.3.0-beta.17

zackarychapple avatar Jul 29 '14 18:07 zackarychapple

Still happen on rc.0

Foxandxss avatar Sep 13 '14 15:09 Foxandxss

So, we tried this a while ago --- the thing is it was reverted: 308598795af75c33a966ecb3124f4a640d72458d

Basically the issue is, ngView will instantiate a route, but won't update immediately --- the ngView's template content is empty until the route updates with a template.

It does work if the route update happens, however (http://plnkr.co/edit/MoqyggpCnCoSAAq97359?p=preview)

So, the reason this fix was reverted was, it means we end up emitting events which were maybe not expected to be emitted, or something. I don't have the details on the specific failures that caused the revert, but we might be able to re-land it, because that's essentially the only thing that breaks this.

edit

This pull request broke some of our code due to a change in the ordering of the onLocationSuccess event and the route resolve logic. In the past, onLocationSuccess was always fired before the route's resolve method was called. As of this change, onLocationSuccess fired after the resolve method when navigating to a new page (though no change was observed on initial page load).

caitp avatar Sep 13 '14 16:09 caitp

ng 1.3.8, reload fix worked for me :)

luckylooke avatar Jan 10 '15 19:01 luckylooke

I believe this is also requiring me to do some silly stuff in my unit tests as a workaround:

http://stackoverflow.com/q/29170970/1259275

longrunningprocess avatar Mar 20 '15 16:03 longrunningprocess

This bug still exists in 1.3.15

by adding the following code it's fixed app.run(['$route', function($route) { $route.reload(); }]);

dvdknaap avatar May 15 '15 06:05 dvdknaap

The bug still exists in 1.4.1 as well. We have deeply nested ng-include that has a 'page' directive which contains ng-view in the template.

It seems just the act of injecting $route into the run block does the trick for us. Someone else pointed that out earlier in thread, "tobyn commented on Sep 6, 2013" but the noop isn't necessary just the injection.

// doesn't work!
app.run(['$log', '$rootScope', function ($log, $rootScope) { 
  // nothing
}
// works!
app.run(['$log', '$rootScope', '$route', function ($log, $rootScope, $route) { 
  // nothing
}

blackbeltdev avatar Jun 18 '15 17:06 blackbeltdev

+1 to blackbeltdev's comment.

mstroming avatar Jun 18 '15 17:06 mstroming

There's an interesting sentence in here which might explain why injecting the $route fixes the problem with ng-include.

http://www.bennadel.com/blog/2770-route-must-be-injected-in-order-to-enable-the-routechangesuccess-event-in-angularjs.htm

"Instead, I have another, optional, sub-controller which does nothing but inject the $route service (which forces AngularJS to call the $routeProvider)."

blackbeltdev avatar Jun 19 '15 00:06 blackbeltdev