如何构造下面的指令,以便我可以访问函数ul
中的所有元素link
。
在下面的代码中,如果我查看elm
(登录到控制台),它是注释类型,ul 是兄弟姐妹。我如何构造一个指令,以便 ul 成为函数elm
中的子节点link
(指令附加到的位置)。如果那不可能,如何获得所有兄弟姐妹。所以,我想渲染嵌套列表 (ul, li) 但还需要访问ul
链接函数中的每个。
谢谢您的帮助。
[comment, ready: function, toString: function, eq: function, push: function, sort: function…]
0: comment
baseURI: "http://run.plnkr.co/PSOXzsCDf5Re0e4S/"
childNodes: NodeList[0]
data: " ngRepeat: d in data "
firstChild: null
lastChild: null
length: 21
localName: null
namespaceURI: null
nextElementSibling: ul.cont.ng-scope
nextSibling: ul.cont.ng-scope
ng-1379388042747: 6
nodeName: "#comment"
nodeType: 8
nodeValue: " ngRepeat: d in data "
代码:http ://plnkr.co/edit/oGwJNN?p=preview
'use strict';
var app = angular.module('Directives', []);
app.controller('MainCtrl', function ($scope) {
$scope.data = [
{
cont: {id:1},
children:[
{id:1}, {id:2}, {id:3}
]
},
{
cont: {id:2},
children:[
{id:1}, {id:2}, {id:3}
]
}
];
});
app.directive('test', function ($timeout) {
return {
template: '<ul class="cont" ng-repeat="d in data">' +
'<li class="ch" ng-repeat="node in d.children">' +
'<span class="span2">' + 'id - {{node.id}}' + '</span>' +
'</li>' +
'</ul>',
replace: true,
restrict: 'E',
scope: {
data: '=',
conf: '='
}
,
link: function (scope, elm, attrs) {
console.log(elm)
}
};
});