$apply is already in progress.$digest is already in progress. <!doctype html> <html ng-app="Demo"> <head> <meta charset="utf-8" /> </head> <body> <ul ng-controller="ListController"> <!--     src- --> <li ng-repeat="image in images"> <p>Loaded: {{ image.complete }}</p> <img ng-src="{{ image.source }}" bn-load="imageLoaded( image )" /> </li> <!--     src- --> <li> <p>Loaded: {{ staticImage.complete }}</p> <img src="4.png" bn-load="imageLoaded( staticImage )" /> </li> </ul> <!--  jQuery  AngularJS --> <script type="text/javascript" src="//code.jquery.com/jquery-1.9.0.min.js"></script> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script> <script type="text/javascript"> //    var Demo = angular.module( "Demo", [] ); //     Demo.controller("ListController", function( $scope ) { //     $scope.imageLoaded = function( image ) { image.complete = true; }; //   .     //   data-URIs,     $scope.images = [{ complete: false, source: "1.png" }, { complete: false, source: "2.png" }, { complete: false, source: "3.png" }]; //    $scope.staticImage = { complete: false, source: "4.png" }; }); //  ,      Demo.directive( "bnLoad", function() { //   DOM   . function link( $scope, element, attributes ) { //      // $digest     $apply() function handleLoadSync() { logWithPhase( "handleLoad - Sync" ); $scope.$eval( attributes.bnLoad ); } //     //  $digest,     //    function handleLoadAsync() { logWithPhase( "handleLoad - Async" ); $scope.$apply( function() { handleLoadSync(); }); } //         . function logWithPhase( message ) { console.log( message, ":", $scope.$$phase ); } // ,     . //       //    Data URI, //         if ( element[0].src && element[0].complete ) { handleLoadSync(); //     -    // (..    ). } else { element.on( "load.bnLoad", handleLoadAsync ); } //       //  ,   //       attributes.$observe("src", function( srcAttribute ) { logWithPhase( "$observe : " + srcAttribute ); }); //      //       . // :        ; //       . $scope.$watch("( image || staticImage ).complete", function( newValue ) { logWithPhase( "$watch : " + newValue ); }); //      $scope.$on("$destroy", function() { element.off( "load.bnLoad" ); }); } //    return({ link: link, restrict: "A" }); } ); </script> </body> </html> handleLoad - Sync: $ apply $ observe: 4.png: $ digest $ watch: true: $ digest $ observe: 1.png: $ digest $ watch: false: $ digest $ observe: 2.png: $ digest $ watch: false: $ digest $ observe: 3.png: $ digest $ watch: false: $ digest $ observe: 1.png: $ digest $ observe: 2.png: $ digest $ observe: 3.png: $ digest handleLoad - Async: null handleLoad - Sync: $ apply $ watch: true: $ digest handleLoad - Async: null handleLoad - Sync: $ apply $ watch: true: $ digest handleLoad - Async: null handleLoad - Sync: $ apply $ watch: true: $ digest
Source: https://habr.com/ru/post/188760/
All Articles