$ location.path()更新后,第二次点击? [英] $location.path() updates after the second click?

查看:310
本文介绍了$ location.path()更新后,第二次点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[和第二个问题(见下文) - rootScope变量在一个控制器中不可见设置在第二,同级控制器]

可能重复: angularjs $ location.path只在第二次点击更新网址 - 问题的原因,答案似乎并不在我,更基本的,情况相关的

Possible Duplicate: angularjs $location.path only updates URL on second click - the cause of the problem and the answer does not seem relevant in my, more basic, situation

由于每angularjs文档( https://docs.angularjs.org/guide/ $位置):

As per the angularjs docs (https://docs.angularjs.org/guide/$location):

[The $location service]
Maintains synchronization between itself and the browser's URL when the user
...
Clicks on a link in the page.

我明白了$ location.path()反映在浏览器当前的URL,但是当我点击一个链接改变视图,表现出奇怪的行为: $ location.path()不变了第一次时候每次点击一个链接,然后它会更改为被点击了previous时间链接

要看到这样去这里: http://jsfiddle.net/7Ah2W/

我尝试一种解决方法一息尚存,我必须使用二传手超载手动设置$ location.path()。搜索结果

I attempted a workaround whereby I must manually set $location.path() using the setter overload.

在这样做时,我发现了另一个缺陷在我angularjs的理解。我试图在rootScope设置变量来反映当前的路径。这个想法是,意见将自动检测在可变和更新的变化。难道不是每一个范围从rootScope继承?

In doing so, I found another flaw in my understanding of angularjs. I tried setting a variable in the rootScope to reflect the 'current path.' The idea is that views would automatically detect the change in the variable and update. Does not every scope inherit from rootScope?

下面是一个的jsfiddle

为什么我的期望是$ rootScope.currentPath,在NavCtrl被更改,并在CtrlTwo没有得到满足?

Why is my expectation that $rootScope.currentPath, being changed in 'NavCtrl' and updated in 'CtrlTwo' not being met?

我的最终目标是让我的导航栏自动点击视图中的链接时发生改变。类似 https://console.developers.google.com :如果你点击你的项目,导航到左修改API和放大器;验证,设置等。

My end goal is to have my navigation bar automatically change when a link in the view is clicked. Similar to https://console.developers.google.com where if you click your project, the navigation to the left changes to API&Auth, settings, etc.

推荐答案

这似乎永远是一大截的原因是,您所访问的 $ location.path()实际的角度页面过程之前可以运行。奇怪的是,如果你只是添加 $超时与连0毫秒的延迟,它会按预期工作。

The reason it seems to always be "one behind" is that you're accessing the $location.path() before the actual angular page process can run. Strangely enough if you just add a $timeout with even 0ms delay, it'll work as intended.

    $timeout(function () {
        $scope.currentPath = $location.path();
    }, 0);

的jsfiddle例如

jsFiddle example

$ rootScope是全球范围内,而不是在常规的$范围基本上是控制器和放大器之间的胶;视图。

$rootScope is the global scope, as opposed to the regular $scope which is basically the glue between the controller & view.

例如我设置 $ rootScope.test = 123; 在你的第一个控制器,并在第二个控制器我提醒变量,并得到结果。 的jsfiddle $ rootScope例如 。小心$ rootScope,它创建全局范围变量

For example I set $rootScope.test = 123; in your first controller, and in the second controller I alert that variable, and get the result. jsFiddle $rootScope example. Be careful with $rootScope, it creates globally scoped variables

这篇关于$ location.path()更新后,第二次点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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