4
我開發了一個經典的遞歸菜單,它自身很好。問題在於ngClick永遠不會從遞歸指令的元素中被觸發。 我知道這是一個範圍問題,我嘗試了很多配置,但沒有運氣。ngClick不會在遞歸模板中觸發
這是一個小提琴看到它:http://jsfiddle.net/PM2dy/2/
要調用的函數是retrieveFiles(路徑)
,代碼:
app.directive("mediaTree", function ($compile) {
return {
restrict: "E",
scope: { folder: '=', retrieveFiles: '=' },
template:
'<ul>' +
'<li data-ng-repeat="child in folder.Children">' +
'<a href="#" data-ng-click="retrieveFiles(child.Path)">{{child.Name}}</a>' +
'<media-tree folder="child"></media-tree>' +
'</li>' +
'</ul>',
compile: function (tElement, tAttr) {
var contents = tElement.contents().remove();
var compiledContents;
return function (scope, iElement, iAttr) {
if (!compiledContents) {
compiledContents = $compile(contents);
}
compiledContents(scope, function (clone, scope) {
iElement.append(clone);
});
};
}
};
});
app.directive('mediaPanel', function ($compile) {
return {
restrict: 'E',
replace: true,
template: '<div></div>',
link: function (scope, elem, attrs) {
scope.retrieveFiles = function (me) {
console.log("thanks for calling " + me);
}
scope.folder = {
Name: 'Media',
Path: '/',
Children: [
{
Name: 'Child1',
Path: '/Child1',
Children: [
{
Name: 'GrandChild1',
Path: '/Child1/GrandChild1',
Children: []
}
]
},
{
Name: 'Child2',
Path: '/Child2',
Children: [
{
Name: 'GrandChild2',
Path: '/Child1/GrandChild2',
Children: []
}
]
}
]
};
elem.append($compile("<media-tree class='media-tree' folder='folder'></media-tree>")(scope));
}
}
});
哇,它的工作,謝謝! –