Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS routing keeps redirecting back to itself like it's in a loop

Tags:

angularjs

I have

<!DOCTYPE html>
<html lang="en" data-ng-app="myCustomApp">
    <body>
        <div id="body">

           <div ng-view></div>

        </div>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js" type="text/javascript"></script>
    <script src="/Js/app.js"></script>
    <script src="/Js/controllers/controllers.js"></script>

    </body>
</html>

With the following

app.controller('SearchController', function ($scope) {
    init();
    function init() {
        alert("called");
    }
});

and the app declared like below:

var app = angular.module('myCustomApp', []);

    app.config(function($routeProvider) {
        $routeProvider
            .when('/search',
                {
                    controller: 'SearchController',
                    templateUrl: '/js/partials/Search.html'
                })
            .otherwise({ redirectTo: '/search' });

    });

The problem is that the page when browsing to the following it keeps refreshing the page like it's in a loop. Any ideas?

/#/search

like image 945
Mantisimo Avatar asked Aug 12 '13 11:08

Mantisimo


1 Answers

 var app = angular.module('myCustomApp', ['ngRoute']);

Routing is not native in angular you need to add it in as a module.

https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-route.js

This is the CDN for the script for it. I'd really suggest having a look at ui-router works very similarly but uses state or states to manage either the view or sections of the view.

https://github.com/angular-ui/ui-router

The documentation is really good and there are some great examples around.

like image 92
Darren Corbett Avatar answered Oct 07 '22 02:10

Darren Corbett