即使应用后,AngularJs路径也不会改变 [英] AngularJs path not changing, even after apply

查看:74
本文介绍了即使应用后,AngularJs路径也不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于通过自定义指令更改路由的问题。我设置了菜单栏指令并设置了链接功能。此函数中的所有内容均正常运行,但通过 $ location.path 更改URL的方法无效。即使使用 $ rootScope.apply ,它也不会更改。

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

            }
        }
    });
});

为了清楚起见,我使用 requirejs 和定义了$ location和 $ rootScope 。奇怪的是, $ location.path()在替换给出空路径之前。同样,活动类的放置也按预期工作。

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.

谢谢。

推荐答案

尝试使用$ timeout-

try using $timeout -

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

这篇关于即使应用后,AngularJs路径也不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆