Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS $location.path(...) not firing route controller

So I'm trying to update the path on form submit using

$location.path('/search');

But it isn't triggering the route registered to '/search' I've tried with a trailing slash too. Nothing, I've also tried $scope.$apply but I just get the $apply already in progress error so there's definitely a scope.

Why wouldn't this call the controller registered to the route or load the templateUrl registered to it.

Router

App.config(function ($routeProvider, $locationProvider) {

    $locationProvider.html5Mode(true).hashPrefix('!');

    $routeProvider
        .when("/", {
                "controller"  : "HomeController",
                "templateUrl" : "templates/home.html"
        })
        .when("/search", {
                "controller"  : "SearchResultsController",
                "templateUrl" : "templates/search-results.html"
        })
        .when("/search/:location", {
                "controller"  : "SearchLocationController",
                "templateUrl" : "templates/search-results.html"
        })
        .otherwise({
                "redirect" : "/"
        });
});

Form ng-submit callback

$scope.doSearchRequest = function (event, params) {
    // Prevent the default action
    event.preventDefault();
    $scope.data = params;

    $location.path('/search/');
};

edit

Adding this

$scope.$on('$routeChangeStart', function(next, current) { 
           $console.log('$routeChangeStart', arguments);
        });

Just before the $location.path call shows that the route doesn't start to change. Is this a bug in Angular 1.2.5?

like image 245
Dave Mackintosh Avatar asked Dec 16 '13 09:12

Dave Mackintosh


1 Answers

So it turns out I actually needed to have an ng-view somewhere on the page for the whole system to work.

Seems a bit screwy but it works.

like image 193
Dave Mackintosh Avatar answered Sep 22 '22 18:09

Dave Mackintosh