gpt4 book ai didi

javascript - 如何修复 ng-click 在递归指令模板中不起作用

转载 作者:行者123 更新时间:2023-11-29 15:31:55 25 4
gpt4 key购买 nike

我在递归指令模板中使用 ng-click。但不起作用。

这是代码 https://jsfiddle.net/qxz7506n/

当我单击“root 加载更多按钮”时,它起作用了,但“子加载更多按钮”不起作用,以及如何将 ngclick through 指令中的子对象传递到 Controller 范围并对其进行控制。

HTML

<div ng-app='demoA'>
<div ng-controller="IndexCtrl">
<collection collection='tasks' reply-msg="replyMsg(data)" load-more="loadMore()">
</collection>
</div>
</div>

收集指令

.directive('collection', function () {
return {
restrict: "E",
replace: true,
scope: {
collection: '=',
member: '=',
replyMsg: '&',
loadMore: '&'
},
template: "<ul><member ng-repeat='member in collection track by $index' floor='$index + 1' member='member' reply-msg='replyMsg({data: member.name})'></member><li><input type='button' value='load more...' ng-click='loadMore()' /></li></ul>"
}
})

成员指令

.directive('member', function ($compile) {
return {
restrict: "E",
replace: true,
scope: {
floor: '=',
member: '=',
replyMsg: '&',
loadMore: '&'
},
template: "<li>" +
"<div ng-if='member.isReply'>#{{floor}}</div>" +
"<div>{{member.name}}</div>" +
"<div ng-if='!member.isReply'><input type='button' value='Reply' ng-click='replyMsg({data: member.name})'/></div>" +
"</li>",
link: function (scope, element, attrs) {
if (angular.isArray(scope.member.children)) {
element.append("<collection collection='member.children' reply-msg='replyMsg({data: member.name})' load-more='loadMore()'></collection>");
$compile(element.contents())(scope)
}
}
}
})

例如,

当我单击“root 加载更多按钮”时,它通过指令将 Controller 范围和控制台作为对象包括“欧洲”和“南美洲”。

当我单击“子加载更多按钮”时,它通过指令将 Controller 范围和控制台作为对象包括“意大利”和“西类牙”。

最佳答案

我想我修复了它,请参阅 this更新了 JSFiddle。

问题是您没有将 loadMorereplyMsg 函数作为对子指令的引用传递。相反,您可以像这样调用它们:load-more="loadMore()"。这应该只在 ng-click 中完成。我还在传递的方法的指令中将您的 '&' 更改为 '='

HTML

<div ng-app='demoA'>
<div ng-controller="IndexCtrl">
<collection collection='tasks' reply-msg="replyMsg" load-more="loadMore">
</collection>
</div>
</div>

JavaScript

angular.module('demoA', [])
.directive('collection', function () {
return {
restrict: "E",
replace: true,
scope: {
collection: '=',
member: '=',
replyMsg: '=',
loadMore: '='
},
template: "<ul>" +
"<member ng-repeat='member in collection track by $index' floor='$index + 1' member='member' reply-msg='replyMsg' load-more='loadMore'></member>" +
"<li><input type='button' value='load more...' ng-click='loadMore()' /></li>" +
"</ul>"
}
})
.directive('member', function ($compile) {
return {
restrict: "E",
replace: true,
scope: {
floor: '=',
member: '=',
replyMsg: '=',
loadMore: '='
},
template: "<li>" +
"<div ng-if='member.isReply'>#{{floor}}</div>" +
"<div>{{member.name}}</div>" +
"<div ng-if='!member.isReply'><input type='button' value='Reply' ng-click='replyMsg({data: member.name})'/></div>" +
"</li>",
link: function (scope, element, attrs) {
if (angular.isArray(scope.member.children)) {
element.append("<collection collection='member.children' reply-msg='replyMsg' load-more='loadMore'></collection>");
$compile(element.contents())(scope)
}
}
}
})

.controller('IndexCtrl', function ($scope) {
$scope.replyMsg = function(data) {
alert(data);
};
$scope.loadMore = function() {
alert("clicked");
};
$scope.tasks = [
{
name: 'Europe',
children: [
{
name: 'Italy',
isReply: true,
},
{
name: 'Spain',
isReply: true,
}
]
},
{
name: 'South America',
children: []
}
];
});

关于javascript - 如何修复 ng-click 在递归指令模板中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33963620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com