如何prevent AngularJs不必对DOM旧视图和新视图时,路由变化视图、路由、prevent、AngularJs

2023-09-14 00:26:50 作者:默〞

我使用的指令滑动,它创建了一个滑出式,并拥有一个切换。这个价值不是由我写的C $ C,但它表明一个更大的问题对我来说。当我改变看法(通常/用户/:ID类型),滑动是在模板上使用的指令。它的连接功能中的一个元素的指令进行搜索,并绑定一个click事件。问题是,当我改变路线和新的视图(同一类型但不同的ID)被加载的指令是重新绑定到旧观点。如果我的联系过程中停止在Chrome浏览器的话,我会看到在DOM 2 NG-意见和问题是它绑定到离开的人。

I am using a directive "slideable" which creates a slideout area and has a toggle. This code that was not written by me but it demonstrates a larger issue for me. When I changing views (most commonly /user/:id type), slideable is a directive used on the template. The directive searches for an element during its link function and binds a click event. The issue is that when I am changing routes and the new view ( same type but different id ) is being loaded the directive is re-binding to the old view. If I stop the browser in chrome during the link then I will see two ng-views on the dom and the issue is it binds to the one that is leaving.

我也有可能在与此相关的现象等问题。这是正常的,旧的观点仍然是对DOM而新的观点正在制定?为什么难道不新一被渲染以前的老视被破坏?我如何解决这个问题,在这样的指令?

I also have other issues that appear to be related to this phenomenon. Is it normal that the old view would still be on the dom while the new view is being formulated?? Why wouldnt the old-view be destroyed before the new one is rendered? How do I get around this issue in a directive like this?

感谢。

我期待在概念上发生了什么了解。我已经修改了指令,选择最新的观点和适当地搜索和绑定到正确的元素。但我有点困惑,为什么会有对DOM其中两个并存的状态。

I am looking to understand conceptually what is happening. I already modified the directive to select the latest view and to appropriately search and bind to the correct element. But I am a bit perplexed as to why there would be a state where both co-exist on the dom.

推荐答案

一个明确的原因,老的HTML片段是短暂present随着新之一就是支持从旧到新的过渡动画。 看看在 ngView 文件,你会看到一个动画过渡的一个例子,它会很清楚,这是不是一个错误或设计缺陷。

One definitive reason why the old HTML fragment is briefly present along with the new one is to support animation of transitions from the old to the new. Take a look at the ngView documentation and you'll see an example of an animated transition, and it'll be clear that this is not a bug or a design flaw.

通常,当某人具有结合到右元素或元素的事件的问题,这是因为它们是不限制选择的HTML片段的范围被添加或更新的,或试图外定位的DOM部件选择所述元件的指令。所以这是首先要检查的地方,该指令被正确地做事,但就像我说的,我们需要code,检查这一点。

Usually when someone has problems with binding to the right element or element's event, it's because they are selecting the element without limiting the scope of the selector to the HTML fragment being added or updated, or trying to target parts of the DOM outside of the directive. So that's the first place to check, that the directive is doing things right, but like I said we'll need code to check on that.