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

1.5.0 has the same behavior. Requires you to inject $route into run(..)

erikcode avatar Feb 18 '16 08:02 erikcode

The fix was reverted in https://github.com/angular/angular.js/commit/75bf80700e992e6d60f16f6360d400ad28049d36

Then I suppose the issue should be re-opened.

davidsandoz avatar Apr 21 '16 14:04 davidsandoz

It will be re-applied for v1.6.x, with a breaking change notice and possibly a way to disable the feature is tests.

We just have to make sure the BC notice is added to the CL when the time comes.

Narretz avatar Apr 21 '16 21:04 Narretz

+1 to blackbeltdev's comment. =) Works for 1.5.x

goddabuzz avatar May 02 '16 19:05 goddabuzz

this is still not fixed in 1.5.8

HaroldStruwig avatar Sep 12 '16 13:09 HaroldStruwig

@HaroldStruwig, this was fixed on master with #14893 (and will be included in the upcoming v1.6.0 release. The fix can't be backported as-is, due to a slight breaking change.

Although we could backport it, but change the default to to non-eager $route instantiation (to avoid the breaking change), it isn't worth it. Manually fixing this is easier than opting-into eager-instantiation. E.g.:

// IF we had backported the fix (which we won't)
app.config(['$routeProvider', function ($routeProvider) {
  $routeProvider.eagerInstantiationEnabled(true);
}]);

// vs

// Manually fixing it in v1.5.x (much sorter and recommended)
app.run(['$route', angular.noop]);

The work-around/fix for v1.5.x is even document here.

gkalpak avatar Sep 12 '16 14:09 gkalpak