简体   繁体   English

Angular JS $ location.path(...)没有触发路由控制器

[英]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. 但它并没有触发注册到'/search'的路线我也试过了一个尾随斜线。 Nothing, I've also tried $scope.$apply but I just get the $apply already in progress error so there's definitely a scope. 没什么,我也试过$scope.$apply但是我只是得到$apply already in progress错误,所以肯定有一个范围。

Why wouldn't this call the controller registered to the route or load the templateUrl registered to it. 为什么不调用注册到路由的控制器或加载注册到它的templateUrl

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 形成ng-submit回调

$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. 就在$location.path调用之前,显示路由没有开始更改。 Is this a bug in Angular 1.2.5 ? 这是Angular 1.2.5的错误吗?

So it turns out I actually needed to have an ng-view somewhere on the page for the whole system to work. 事实证明,我实际上需要在页面的某个位置使用ng-view来使整个系统正常工作。

Seems a bit screwy but it works. 似乎有点棘手,但它的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM