Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Route Infinite Loop

For some reason when I have a dynamic property in my route and access that page I get stuck in an infinite loop where that page will continuously request itself.

.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider)
{
    $locationProvider.html5Mode(true);

    $routeProvider.when("/", {
        templateUrl: "pages/index.html",
        controller: "IndexCtrl"
    }).when("/listhome", {
        templateUrl: "pages/listhome.html",
        controller: "ListHomeCtrl"
    }).when("/profile", {
        templateUrl: "pages/profile.html",
        controller: "ProfileCtrl"
    }).when("/newlist", {
        templateUrl: "pages/newlist.html",
        controller: "NewListCtrl"
    }).when("/userlists/:id", {
        templateUrl: "pages/userlists.html",
        controller: "UserListsCtrl"
    }).otherwise({
        redirectTo: "/"
    });

The route I'm looking at is the /userlists/:id route. The controller for that route is-

TopTenApp.controller("UserListsCtrl", ["$scope","$routeParams", function($scope, $routeParams)
{
    console.log($routeParams);
    $scope.lists = [];
}]);

And when I access /userlists/9 I see-

Object {id: "9"}

Being logged every 3 seconds and the page freezes. This seems to happen whenever there is a forward slash after the location ("/userslists/" instead of "/userlists").

Does anyone know the cause of this?

like image 855
micah Avatar asked Jan 25 '14 21:01

micah


People also ask

How do I set up routing in AngularJS?

To make your applications ready for routing, you must include the AngularJS Route module: Then you must add the ngRoute as a dependency in the application module: var app = angular.module("myApp", ["ngRoute"]); Now your application has access to the route module, which provides the $routeProvider.

What is ngroute in AngularJS?

The ngRoute module routes your application to different pages without reloading the entire application. What do I Need? To make your applications ready for routing, you must include the AngularJS Route module: Then you must add the ngRoute as a dependency in the application module:

What is infinite data in angular?

Infinite means, the data will keep on loading when a user reaches the bottom of the content. This Angular post is compatible with Angular 4 upto latest versions, Angular 7, Angular 8, Angular 9, Angular 10, Angular 11 & Angular 12 Infinite means, without any limit; In some situations, we have huge data to show on the user screens.

How do I use the $routeprovider with AngularJS expressions?

With the $routeProvider you can also define a controller for each "view". The "london.htm" and "paris.htm" are normal HTML files, which you can add AngularJS expressions as you would with any other HTML sections of your AngularJS application. In the previous examples we have used the templateUrl property in the $routeProvider.when method.


1 Answers

Silly me I realized the problem. I guess it makes sense but the template url needs to have a forward slash in front of it when the page is multiple "directories" deep.

.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider)
{
    $locationProvider.html5Mode(true);

    $routeProvider.when("/", {
        templateUrl: "/pages/index.html",
        controller: "IndexCtrl"
    }).when("/listhome", {
        templateUrl: "/pages/listhome.html",
        controller: "ListHomeCtrl"
    }).when("/profile", {
        templateUrl: "/pages/profile.html",
        controller: "ProfileCtrl"
    }).when("/newlist", {
        templateUrl: "/pages/newlist.html",
        controller: "NewListCtrl"
    }).when("/userlists/:id", {
        templateUrl: "/pages/userlists.html",
        controller: "UserListsCtrl"
    }).otherwise({
        redirectTo: "/"
    });
}]);

Hopefully that helps someone else with a similar problem.

like image 71
micah Avatar answered Sep 22 '22 12:09

micah