gpt4 book ai didi

javascript - AngularJs 路径没有改变,即使在申请之后

转载 作者:行者123 更新时间:2023-11-30 15:50:19 24 4
gpt4 key购买 nike

我有一个关于通过自定义指令改变路由的快速问题。我设置了一个菜单栏指令并设置了一个链接功能。此函数中的所有内容都可以正常工作,但通过 $location.path 更改 URL 却不行。即使在使用 $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() 给出了一个空路径。此外,类“active”的放置按预期工作。

谢谢。

最佳答案

尝试使用 $timeout -

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

关于javascript - AngularJs 路径没有改变,即使在申请之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39447009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com