Chapter 7: Optimizing with the compile phase in ng-repeat

HTML

<script src="https://code.angularjs.org/1.3.2/angular.min.js"></script>
<div ng-app="myApp">
    <div ng-controller="Ctrl">
        <div ng-repeat="item in items">
            <my-directive item="{{item}}">*</my-directive>
        </div>
    </div>
</div>

JavaScript

angular.module('myApp', [])
.controller('Ctrl', function ($scope) {
    $scope.items = [1,2,3,4,5];
})
.directive('myDirective', function($log) {
    return {
        compile: function(el, attrs) {
            // this will be called once
            $log.log('compiled', attrs.item);
            return function link(scope, el, attrs) {
                // this will be called items.length times
                $log.log('linked', attrs.item);
            };
        }
    };
});