使用 AngularJS 重定向
我正在尝试使用以下方法重定向到另一条路线:
I'm trying to redirect to another route using:
$location.path("/route");
但由于某种原因它不起作用.我使用 jQuery-UI 做了一个自动完成小部件,一旦用户选择一个选项,我就会从范围中调用一个函数.我调试了它,它进入了函数,但它从未重定向到另一条路由.只有当我按下一个键时它才会改变路线.
But for some reason it is not working. I did an auto-complete widget using jQuery-UI and I'm calling a function from the scope once the user selects an option. I debugged it and it enters the function but it is never redirected to the other route. It only changes the route when I press a key.
我觉得这有点奇怪,但我还没有想出如何解决这个问题.我用过
I think it is kind of strange but I haven't figured out how to solve this. I used
window.location = "#/route";
它可以工作,但我想使用 path()
函数.
and it works but I want to use the path()
function.
有人知道为什么会这样吗?
Does anybody have any idea why this is happening?
举个不工作代码的例子,回答这个问题很容易,但有了这些信息,我认为你是在 AngularJS 摘要之外调用 $location.path.
With an example of the not-working code, it will be easy to answer this question, but with this information the best that I can think is that you are calling the $location.path outside of the AngularJS digest.
尝试在指令 scope.$apply(function() { $location.path("/route"); });