Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

javascript - Angular JS identify an digest complete event and removing # from url in angular js during viewchange

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.

Popup view and Canvas

Currently i am calling a method from the view

enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update

Karl seamon gave a talk in ng-conf 2014.

In this video (22:20 minute) he talked about a future possiblity of a built-in $postDigestWatch.

Here is an open issue in: 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.

  • 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 callback and register a $watch for the next $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 ,From the docs:

If you want to be notified whenever $digest() is called, you can register a watchExpression function with $watch() with no listener.

$timeout , From here: Defer angularjs watch execution after $digest (raising DOM event)

$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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...