javascript Angular JS 识别摘要完成事件并在视图更改期间从 Angular js 中的 url 中删除 #
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21138388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular JS identify an digest complete event and removing # from url in angular js during viewchange
提问by anandaravindan
1) Is there any digest complete event which I can use to update my canvas. I have an angular app which has view for different properties of the canvas object. Whenever I change the property, once the digest is complete, If I can get the digest complete event I can update the canvas(using kineticJs) to redraw the chart with latest properties.
1) 是否有任何摘要完整事件可用于更新我的画布。我有一个角度应用程序,它可以查看画布对象的不同属性。每当我更改属性时,一旦摘要完成,如果我可以获得摘要完成事件,我可以更新画布(使用 kineticJs)以使用最新属性重新绘制图表。
Currently i am calling a method from the view
目前我正在从视图中调用一个方法
2) I am just using views and routing it to a new view whenever an object settings is opened. In this case the url also change with the webpage/#view.Its just the popup I dont need the #view at the end of the page but to still use the routing and view concept. Is there any otherway.
2)我只是使用视图并将其路由到一个新视图,只要打开一个对象设置。在这种情况下,url 也会随着网页/#view 的变化而变化。它只是弹出窗口,我不需要页面末尾的 #view,但仍然使用路由和视图概念。有没有别的办法。
回答by Ilan Frumer
Update
更新
Karl seamon gave a talk in ng-conf 2014.
Karl seamon 在ng-conf 2014 上发表了演讲。
In this video(22:20 minute) he talked about a future possiblity of a built-in $postDigestWatch.
在这段视频(22:20 分钟)中,他谈到了内置$postDigestWatch的未来可能性。
Here is an open issue in: https://github.com/angular/angular.js/issues/5828
这是一个未解决的问题:https: //github.com/angular/angular.js/issues/5828
So, It will probably get to the core in future releases, until then you can use the trick below.
所以,它可能会在未来的版本中成为核心,在那之前你可以使用下面的技巧。
An example on plunker.
一个关于 plunker的例子。
- A digest cycle may have multiple
$digest
. - I
$watch
for the first$digest
to register a$timeout
which would run after the digest cycle ends. - I must unregister the
$watch
immediately to avoid multiple$timeout
callbacks for one digest cycle. - In the
$timeout
callback I invoke the user callbackand register a$watch
for the next$digest
.
- 一个摘要循环可能有多个
$digest
. - 我
$watch
是第一个$digest
注册一个$timeout
将在摘要周期结束后运行的。 - 我必须
$watch
立即取消注册以避免$timeout
在一个摘要周期内多次回调。 - 在
$timeout
回调中,我调用用户回调并$watch
为下一个$digest
.
Use $watch in conjunction with $timeout:
将 $watch 与 $timeout 结合使用:
function postDigest(callback){
var unregister = $rootScope.$watch(function(){
unregister();
$timeout(function(){
callback();
postDigest(callback);
},0,false);
});
}
postDigest(function(){
console.log('do something');
})
$digest,From the docs:
$digest,来自文档:
If you want to be notified whenever $digest()is called, you can register a watchExpression function with $watch()with no listener.
如果您想在调用$digest()时收到通知,您可以使用$watch()注册一个没有监听器的 watchExpression 函数。
$timeout, From here: Defer angularjs watch execution after $digest (raising DOM event)
$timeout,从这里开始:在 $digest(引发 DOM 事件)之后推迟 angularjs 观察执行
$timeout will cause another digest cycle to be executed after the function is executed. If your trigger does not affect anything Angular, you can set the invokeApply argument to false to avoid running another digest cycle.
$timeout 将导致在函数执行后执行另一个摘要循环。如果您的触发器不影响任何 Angular,您可以将 invokeApply 参数设置为 false 以避免运行另一个摘要循环。
回答by Jake Johnson
as an alternative to what Ilan said, you could use $evalAsync.
作为 Ilan 所说的替代方案,您可以使用 $evalAsync。
from the same docs:
来自相同的文档:
Executes the expression on the current scope at a later point in time.
The $evalAsync makes no guarantees as to when the expression will be executed, only that:
it will execute after the function that scheduled the evaluation (preferably before DOM rendering).
at least one $digest cycle will be performed after expression execution.
Any exceptions from the execution of the expression are forwarded to the $exceptionHandler service.
Note: if this function is called outside of a $digest cycle, a new $digest cycle will be scheduled. However, it is encouraged to always call code that changes the model from within an $apply call. That includes code evaluated via $evalAsync
Also, take a look at this commentin regards to this