AngularJs 路径没有改变,即使在应用之后

AngularJs path not changing, even after apply

我有一个关于通过自定义指令更改路由的快速问题。我设置了一个菜单栏指令并设置了一个 link 函数。此函数中的所有内容都可以正常工作,但是 URL 到 $location.path 的转换不会。即使在使用 $rootScope.apply 之后,它也没有改变。

define([
    '../module',
    '../namespace'
],
function (module, namespace) {
    module.directive(namespace + '.menubarDirective', function ($location,     $rootScope) {
        return {
            restrict: 'EA',
            replace: 'true',
            templateUrl: 'scripts/app/menubar/views/menubar.html',
            scope: {},
            controller: function () {
            },
            link: function (scope, element, attrs) {

                $("#menubarStoreButton").click(function () {
                    $('.active').removeClass('active');
                    $(this).addClass('active');
                    $location.path('/store');
                    $rootScope.$apply();
                })

            }
        }
    });
});

为了清楚起见,我使用 requirejs 并定义了 $location 和 $rootScope。奇怪的是,替换之前的 $location.path() 给出了一个空路径。此外,class 'active' 的位置按预期工作。

谢谢。

尝试使用 $timeout -

$("#menubarStoreButton").click(function() {
    $('.active').removeClass('active');
    $(this).addClass('active');
    $timeout(function() {
        $location.path('/store');
    });
})

我想知道你的路径是否已经 /store 并且你想重新加载你的页面尝试导航尝试 $window.location.href = "/store"; 参考:$location and $window