Angular JS$location.path(..)未启动路由控制器

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

本文关键字:启动 路由 控制器 JS location path Angular      更新时间:2023-09-26

所以我正在尝试使用更新表单提交上的路径

$location.path('/search');

但它并没有触发注册到'/search'的路由,我也尝试过使用尾部斜杠。没什么,我也尝试过$scope.$apply,但我只是得到了$apply already in progress错误,所以肯定有范围。

为什么这不调用注册到路由的控制器或加载注册到它的templateUrl

路由器

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" : "/"
        });
});

表单ng-submit回调

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

编辑

添加此

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

就在$location.path呼叫显示路由没有开始更改之前。这是Angular 1.2.5中的一个错误吗?

所以我实际上需要在页面上的某个地方有一个ng-view,以便整个系统工作。

看起来有点古怪,但它有效。