Angularjs 递归树模板,包括以多维索引为参数的ng click
晚上好, 有没有办法创建一个递归显示结构并在每个项上都有onclick函数的模板。此onclick函数需要包含此元素的所有父索引的当前索引(例如,下面列表中BMW的[0][2][1]) 我当前的指令是这样的,但我不知道如何才能获得ng click函数每个元素的完整索引Angularjs 递归树模板,包括以多维索引为参数的ng click,angularjs,angularjs-directive,angularjs-scope,angularjs-ng-repeat,Angularjs,Angularjs Directive,Angularjs Scope,Angularjs Ng Repeat,晚上好, 有没有办法创建一个递归显示结构并在每个项上都有onclick函数的模板。此onclick函数需要包含此元素的所有父索引的当前索引(例如,下面列表中BMW的[0][2][1]) 我当前的指令是这样的,但我不知道如何才能获得ng click函数每个元素的完整索引 app.directive('collection', function () { return { restrict: "E", replace: true, scope:
app.directive('collection', function () {
return {
restrict: "E",
replace: true,
scope: {
collection: '='
},
template: `
<ul>
<member ng-repeat='member in collection'
member='member' index='$parent.index + $index'>
</member>
</ul>`
}
});
app.directive('member', function ($compile) {
return {
restrict: "E",
replace: true,
template: "<li ng-click=\"select(member)\">{{member.title}}</li>",
link: function (scope, element, attrs) {
var collectionSt = '<collection collection="member.sub"></collection>';
if (angular.isArray(scope.member.sub)) {
$compile(collectionSt)(scope, function(cloned, scope) {
element.append(cloned);
});
}
}
}
});
app.directive('collection',function(){
返回{
限制:“E”,
替换:正确,
范围:{
集合:'='
},
模板:`
`
}
});
应用程序指令('member',函数($compile){
返回{
限制:“E”,
替换:正确,
模板:“{{member.title} ”,
链接:函数(范围、元素、属性){
var collectionSt='';
if(angular.isArray(scope.member.sub)){
$compile(collectionSt)(作用域,函数(克隆,作用域){
元素。追加(克隆);
});
}
}
}
});
在控制器中,向每个成员添加fullIndex
属性,并将该属性作为ng click
函数的参数返回:
template: `<li ng-click="select(member.fullIndex)">{{member.title}}</li>`,
模板:`{{member.title} `,
让模板用AngularJS表达式计算
fullIndex
值会使应用程序难以理解、调试和维护。最好用JavaScript进行计算。使用replace:true
会带来麻烦。看见
template: `<li ng-click="select(member.fullIndex)">{{member.title}}</li>`,