Ionic in $interval and $apply ()

Source: Internet
Author: User

function () {//monitor video playback time     if (media.currenttime>1) {//If the video is longer than 1 seconds      $scope. scoreshow=true;//score box pops up
Console.log ($scope. scoreshow);//true
};
},true);

Add a listener event to a video media object: When Timeupdate, the callback function executes normally, $scope. Scoreshow also becomes true, but on the HTML page

<!--scoring bullet frames--
<
Divclass= "Videomessage"Ng-show= "Scoreshow"> <P>Make a point and leave your attitude!</P> <Pclass= "Btns"> <spanNg-click= "Confirmscore ()">Are you sure</span> <spanNg-click= "Ignorescore ()">Ignore</span> </P></Div>

Video playback to 1 seconds later, the scoring bullet box is always not displayed, and in the page's console to see the Scoring bullet box, display:none! Important

Note that $scope.scoreshow does not actually work.

The strange point is:

If there is a line in the same controller: $interval (function () {},0);

As follows

Media.addeventlistener (' timeupdate ', function() {//Listen for video playback time if     (media.currenttime>1) {//If the video is broadcast for longer than 1 seconds       $scope. Scoreshow=true;//Scoring Box Popup
Console.log ($scope. scoreshow);//true };
},true);
$interval (function() {},0);

The scoring bullet box will be displayed.

However, if you replace the $interval with the native code setinterval, the scoring bullet box will not be displayed.

Focus:

Therefore, infer: because Ionic does not know the native code does some things, so do not know $scope.scoreshow change, to ionic know need to use $scope. $apply () Wrap the $scope.scoreshow=true up:

function () {// monitor video playback time if     (media.currenttime>1) {// If the video broadcast time exceeds 1 seconds             $ Scope. $apply (function() {         $scope. scoreshow  =true;  the score box pops up              })     ; },true);

So $scope.scoreshow not just become true, but also really play a role, the scoring frame shows.

Note:

Quote a passage from http://www.2cto.com/kf/201311/256848.html to explain: when to use $apply ()

So when exactly do we need to call the $apply () method? The situation is very small, virtually all of our code is wrapped in $scope. $apply () inside, like Ng-click,controller initialization, $http destroy function, etc. In these cases, we do not need to call ourselves, in fact, we can not call ourselves, otherwise the $apply () method inside the call $apply () method will throw an error. If we need to run the code in a new execution sequence, we really need to use it, and if and only if the new execution sequence is not created by the method of the angular JS library, we need to use the code in $scope. $apply () Wrap It Up

Ionic in $interval and $apply ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.