Created
March 13, 2015 16:43
-
-
Save tomtwo/0ba6a3d2b4e7115eb876 to your computer and use it in GitHub Desktop.
Angular Directive: Access images generated with ng-repeat from parent directive once all have loaded
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<div ng-repeat="image in images" style="display: inline;"> | |
<img image-directive-item ng-src="{{image.url}}" data-index="{{$index}}" /> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module("myApp") | |
.directive("imageDirective", function() { | |
return { | |
scope: { images: '=images'}, | |
controller: function($scope){ | |
var toBeLoaded = $scope.images.length; | |
this.childLoaded = function(index, childElement) { | |
$scope.images[index].elem = childElement; | |
if(--toBeLoaded == 0) { | |
allChildrenLoaded(); | |
} | |
}; | |
function allChildrenLoaded() { | |
console.log("All children loaded."); | |
console.log($scope.images[0].elem); | |
} | |
}, | |
templateUrl: _contextPath + 'imageDirective.html' | |
}; | |
}) | |
.directive("imageDirectiveItem", function() { | |
return { | |
require: "^imageDirective", | |
link: function(scope, element, attrs, imageDirectiveCtrl) { | |
element.bind("load", function() { | |
var elem = this; | |
var index = parseInt(this.attributes["data-index"].value); | |
scope.$apply(function() { | |
imageDirectiveCtrl.childLoaded(index, elem); | |
}); | |
}); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment