thor
thor

Reputation: 1358

AngularJS infinite loop with ng-view

I've just started using AngularJS for a new app I'm looking at putting together but I've run into a problem when using routes and views.

I've stripped this example down to the bare minimum but the issue remains. All this example is doing is hitting the server and returning the index.html page, which then sources Angular etc.

index.html

<!doctype html>
<html lang="en" ng-app="main">
    <head>
        <meta charset="utf-8" />

        <link rel="stylesheet" type="text/css" src="css/style.css" />

        <script type="text/javascript" src="js/ext/angular.min.js"></script>
        <script type="text/javascript" src="js/ext/angular-route.min.js"></script>
        <script type="text/javascript" src="js/main.js"></script>
        <script type="text/javascript" src="js/test.js"></script>

        <base href="/ui/">
    </head>
    <body>
        <div ng-view></div>
    </body>
</html>

main.js

(function() {
    var app = angular.module('main', ['ngRoute', 'test']);

    app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

        $routeProvider
            .when('/test', {
                templateUrl: 'html/test.html',
                controller: 'TestCtrl'
            })
            .otherwise({
                redirectTo: '/test'
            });

        $locationProvider.html5Mode(true);
    }]);
})();

test.js

(function() {
    var app = angular.module('test', []);

    // get hierarchy
    app.controller('TestCtrl', ['$scope', function($scope) {

        alert('here');

    }]);
})();

test.html

<div>FooBar!</div>

The alert gets fired infinitely but I just don't know why. I've seen other examples where ng-view and routing appear to be used exactly the same way, so I'm not sure what I'm doing wrong...

Upvotes: 2

Views: 4749

Answers (4)

yoshi
yoshi

Reputation: 1110

I had this issue today in March 2016. I have just found out what was causing the infinite loop when ng-view is present in the html (index.html in my case which is the initial html loaded at the start).

Ok, the problem was after all very simple one. I had this route provider setting in my app.js

angular.module('myapp',['ngRoute'])
    .config(function($routeProvider) {
      $routeProvider
      .when('/', {
          templateUrl:'/index.html',
          controller:'MyAppCtrl'
      })

Since the initial html loaded is index.html, and the url at that point is '/', and the routeProvider invokes the code at when '/'. Yes, it loads index.html again, and again and again and again... till it dies. The solution is not to set index.html as the templateUrl for the '/' route. The html (template) should not include <div ng-view></div>.

Upvotes: 2

thor
thor

Reputation: 1358

Ok, I solved my problem. I've accepted sergio's as it was closest to how I realised what the problem was - my app was requesting the html file from the application server, which is set up to return the index.html file as a default action. As the html request had no associated action, the default response of returning index.html was kicking in instead of the test.html file.

Once I changed the url so it was getting the html file from the web server, everything worked great. If I'd taken a moment earlier to actually think through what was happening, it would've been obvious.

Thanks for the responses!

Upvotes: 0

Aero204
Aero204

Reputation: 99

Here's how I've done it, example here

Code

.config(['$routeProvider', '$locationProvider',function($routeProvider, $locationProvider) {
$routeProvider
    .when('/test', {
        template: '<div>test</div>',
        controller: 'testCtrl'
    })
    .when('/other', {
        template: '<div>Delete</div>',
        controller: 'otherCtrl'
    })
    .otherwise({
        redirectTo: '/test'
    });

    $locationProvider.html5Mode(true);

}]);

Upvotes: 0

sergio
sergio

Reputation: 639

I had same problem sometime ago. Please, use firebug or some network control in the same browser at the developers tools panel where you can see the requests to the server for resources and then check that test.html file is requested and is correctly retrieved. It seems like the only one that is retrieved is the index.html and due this, the loop.

Probably you have to use this templateUrl value "/html/test.html" with "/" before. To localize this resource.

This is the idea that I'm proposing you. Localize the test.html resource with the correct way. I hope this can help you.

Upvotes: 6

Related Questions