我尝试实现angular ng-repeat指令,但我不明白为什么此代码无法正常工作。
.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);
});
}
});
}
}
}
})
和HTML片段
<ul ng-controller="MyCtrl">
<li my-repeat="city in cities">{{city.name}}</li>
</ul>
我的问题是LI元素显示为正常,但它们不包含城市名称。请解释一下为什么会这样。我了解ng-transclude在原始情况下的工作方式,当我们有带有ng-transclude元素的模板并且在指令定义中指定transclude:true时,但是我不明白在transclude:“element”中如何工作。
P.S.对不起我的英语不好。我是初学者:)
最佳答案
我注意到您将indexString写入控制台时不正确。更改:match = myLoop.match(/^\s*(.+)+in\s+(.*?)\s*(\s+track\s+by\s+(.+)\s*)?$/)
到match = myLoop.split(' ')
适用于我的完整代码:
var app = angular.module('app', []);
app.controller("MyCtrl", function($scope){
$scope.cities = [{
name:'a'
}, {name: 'b'},
{name: 'c'}]
})
app.directive("myRepeat", function() {
return {
transclude: "element",
priority: 1000,
compile: function(tElem, tAttrs) {
var myLoop = tAttrs.myRepeat,
match = myLoop.split(' '),
indexString = match[0],
collectionString = match[2],
parent = tElem.parent();
console.log("match: " + match);
console.log("index string: " + indexString);
console.log("coll: " + collectionString);
var elements = [];
return function($scope, iElem, iAttrs, controller, $transclude) {
$scope.$watchCollection(collectionString, function(newCollection) {
var i;
// 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);
});
}
});
}
}
}
})