AngularJs path not changing, even after apply

无人久伴 提交于 2019-12-11 04:53:31

问题


I have a quick question about the chaning of a route trough a custom directive. I set up a menubar diretive and set up a link function. Everything in this function works correctly, but the chaning of the URL trough the $location.path does not. Even after using $rootScope.apply, it does not change.

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();
                })

            }
        }
    });
});

To be clear, I use requirejs and $location and $rootScope are defined. The weird thing is, $location.path() before the replace gives a empty path. Also, the placement of the class 'active' works as intended.

Thanks.


回答1:


try using $timeout -

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



回答2:


I wonder if your path is already /store and you want to reload your page try navigate try $window.location.href = "/store"; ref: $location and $window



来源:https://stackoverflow.com/questions/39447009/angularjs-path-not-changing-even-after-apply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!