我正在尝试使用在指令中定义的元素模板绑定click函数。不知怎的,它不是因为未知的原因而工作,也没有抛出任何错误。
我的HTML
<body>
<div ng-app="mainApp">
<div ng-controller="MyController">
<div id="Container" class="Container">
<collection collection='testdata'></collection>
</div>
</div>
</div>
</body>
我的JS
var mainApp = angular.module("mainApp", [])
mainApp.directive('collection', function () {
return {
restrict: "E",
replace: true,
scope: {
collection: '='
},
template: "<ul><member ng-repeat='member in collection' member='member'></member></ul>"
}
})
mainApp.directive('member', function ($compile) {
//var NewImg = "<img ng-click=ShowChildren() src=../Images/Plus15.png></img>";
//var NewChild = "<li>"+NewImg+"<span>{{member.TagName}}</span></li>";
//var NewChild = "<li><img id=dynamic src=../Images/Plus15.png></img><span>{{member.TagName}}</span></li>";
var NewChild = "<li><img ng-click=ShowChildren(event) src=../Images/Plus15.png></img><span>{{member.TagName}}</span></li>";
//var NewChild = "<li>{{member.TagName}}</li>";
return {
restrict: "E",
replace: false,
scope: {
member: '='
},
template: NewChild,
link: function (scope, element, attrs) {
var collectionSt = '<collection collection="member.children"></collection>';
if (angular.isArray(scope.member.children)) {
element.append("<collection collection='member.children'></collection>");
$compile(element.contents())(scope.$new())
//$compile(collectionSt)(scope, function(cloned, scope) {
//element.append(cloned);
//});
}
}
}
})
mainApp.controller('MyController', function ($scope) {
$scope.data = [{"TagName":"MyRootNode","TagPath":">MyRootNode","children":[{"TagName":"LandXML","TagPath":">MyRootNode>ChildItems>LandXML","children":[{"TagName":"Units","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Units","children":[{"TagName":"Imperial","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[1]>Imperial","children":[]},{"TagName":"Project","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Project","children":[]},{"TagName":"Application","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Application","children":[{"TagName":"Author","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[2]>Author","children":[]},{"TagName":"Alignments","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Alignments","children":[]},{"TagName":"Roadways","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Roadways","children":[{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[1]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[2]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[3]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[4]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[5]","children":[]}]}]}]}]},{"TagName":"Surfaces","TagPath":">MyRootNode>ChildItems>Surfaces","children":[{"TagName":"Surface1","TagPath":">MyRootNode>ChildItems>ChildItems[2]>Surface1","children":[]},{"TagName":"Surface2","TagPath":">MyRootNode>ChildItems>ChildItems[2]>Surface2","children":[]}]}]}]
$scope.testdata = $scope.data[0].children
});
function ShowChildren (event) {
console.log("in function ShowChildren");
var src = ($(this).attr('src') === '../Images/Plus15.png')
? '../Images/Minus15.png'
: '../Images/Plus15.png';
$(this).attr('src', src);
event.stopImmediatePropagation();
}
我注意到,函数ShowChildren在将其置于指令中时执行但是它再次在“$(this).attr('src')”中出现问题。我希望它在单独的文件中,因为我将所有代码移动到不同的JavaScript文件。
请帮我解决这个问题。
答案 0 :(得分:1)
您的ShowChildren()
功能超出了控制器的范围。将该功能放入控制器。
<强>控制器强>
mainApp.controller('MyController', function ($scope) {
$scope.data = [{"TagName":"MyRootNode","TagPath":">MyRootNode","children":[{"TagName":"LandXML","TagPath":">MyRootNode>ChildItems>LandXML","children":[{"TagName":"Units","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Units","children":[{"TagName":"Imperial","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[1]>Imperial","children":[]},{"TagName":"Project","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Project","children":[]},{"TagName":"Application","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Application","children":[{"TagName":"Author","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[2]>Author","children":[]},{"TagName":"Alignments","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Alignments","children":[]},{"TagName":"Roadways","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Roadways","children":[{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[1]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[2]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[3]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[4]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[5]","children":[]}]}]}]}]},{"TagName":"Surfaces","TagPath":">MyRootNode>ChildItems>Surfaces","children":[{"TagName":"Surface1","TagPath":">MyRootNode>ChildItems>ChildItems[2]>Surface1","children":[]},{"TagName":"Surface2","TagPath":">MyRootNode>ChildItems>ChildItems[2]>Surface2","children":[]}]}]}]
$scope.testdata = $scope.data[0].children;
$scope.ShowChildren= function(event) {
console.log("in function ShowChildren");
var src = ($(this).attr('src') === '../Images/Plus15.png')
? '../Images/Minus15.png'
: '../Images/Plus15.png';
$(this).attr('src', src);
event.stopImmediatePropagation();
}
});
<强>指令强>
var NewChild = "<li><img ng-click=ShowChildren($event) src=../Images/Plus15.png></img><span>{{member.TagName}}</span></li>";
答案 1 :(得分:1)
使用ng-click=ShowChildren($event)
代替ng-click=ShowChildren(event)
,您的函数也应在角度范围内定义。由@Kaustubh Khare