I try implement angular ng-repeat directive and I don't understand why this code not work right.
.directive("myRepeat", function() {
return {
transclude: "element",
priority: 1000,
compile: function(tElem, tAttrs) {
var myLoop = tAttrs.myRepeat,
match = myLoop.match(/^\s*(.+)+in\s+(.*?)\s*(\s+track\s+by\s+(.+)\s*)?$/),
indexString = match[1],
collectionString = match[2],
parent = tElem.parent();
return function($scope, iElem, iAttrs, controller, $transclude) {
$scope.$watchCollection(collectionString, function(newCollection) {
var i, block, elements = [];
// check if elements have already been rendered
if (elements.length) {
// if so remove them from DOM, and destroy their scope
for (i = 0; i < elements.length; i++) {
elements[i].el.remove();
elements[i].scope.$destroy();
}
elements = [];
}
for (i = 0; i < newCollection.length; i++) {
$transclude(function(clone, scope) {
scope[indexString] = newCollection[i];
parent.append(clone);
block = {};
block.el = clone;
block.scope = scope;
elements.push(block);
});
}
});
}
}
}
})
and HTML fragment
<ul ng-controller="MyCtrl">
<li my-repeat="city in cities">{{city.name}}</li>
</ul>
My problem is that LI elements rendered normal, but they are not contain city name. Please explain me why so occurs. I understand how work ng-transclude in primitive case, when we have template with element with ng-transclude and in our directive definition specify transclude: true, but I don't understand how that work with transclude: "element". P.S. Sorry for my english. I beginner :)
$transcludefromcompileis deprecated. See docs.angularjs.org/api/ng/service/$compileng-repeathere (it uses the non-deprecated form of thetranscludefn). See this plunker comparing that implementation (which works) to yours (hopefully you can see the difference).