Angular JS识别摘要完成事件,并在视图切换期间从角度js中删除URL

问题描述 投票:7回答:2

1)是否有任何摘要完成事件,我可以用它来更新我的画布。我有一个角度应用程序,它具有画布对象的不同属性的视图。每当我更改属性时,一旦摘要完成,如果我可以获得摘要完成事件,我可以更新画布(使用kineticJs)重绘具有最新属性的图表。

目前我正在从视图中调用方法

2)我只是在打开对象设置时使用视图并将其路由到新视图。在这种情况下,网址也随着网页/ #view而改变。它只是弹出窗口,我不需要页面末尾的#view,但仍然使用路由和视图概念。还有别的吗?

javascript angularjs kineticjs
2个回答
24
投票

Update

Karl seamon在ng-conf 2014发表了演讲。

在这个video(22:20分钟),他谈到了内置$ postDigestWatch的未来可能性。

这是一个未解决的问题:https://github.com/angular/angular.js/issues/5828

所以,它可能会在未来的版本中找到核心,直到那时你可以使用下面的技巧。


An example on plunker.

  • 消化周期可能有多个$digest
  • $watch为第一个$digest注册了一个$timeout,它将在消化周期结束后运行。
  • 我必须立即取消注册$watch以避免一个摘要周期的多个$timeout回调。
  • $timeout回调中,我调用用户回调并为下一个$watch注册$digest

Use $watch in conjunction with $timeout:

function postDigest(callback){    
  var unregister = $rootScope.$watch(function(){  
    unregister();
    $timeout(function(){
      callback();
      postDigest(callback);
    },0,false);       
  });
}

postDigest(function(){
  console.log('do something');
})

$ digest,来自docs

如果您希望在调用$ digest()时收到通知,则可以使用$ watch()注册watchExpression函数而不使用侦听器。

$ timeout,从这里:Defer angularjs watch execution after $digest (raising DOM event)

$ timeout将导致在执行函数后执行另一个摘要循环。如果您的触发器不影响任何Angular,您可以将invokeApply参数设置为false以避免运行另一个摘要周期。


0
投票

作为Ilan所说的替代方案,你可以使用$ evalAsync。

来自同一个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

另外,请看一下this comment

© www.soinside.com 2019 - 2024. All rights reserved.