6

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

can you leverage your answer deeply, you mean you didn't include ng-view before? Or you just placed it in root? Because i have ng-view, but when route starts from 1 and goes to 2, and then go back to 1, controller either view doesn't load

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.