即使应用后,AngularJs路径也不会改变 [英] AngularJs path not changing, even after apply
问题描述
我有一个关于通过自定义指令更改路由的问题。我设置了菜单栏指令并设置了链接功能。此函数中的所有内容均正常运行,但通过 $ 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屋!