Javascript 角度:如何更新递归指令范围/模型
我使用递归指令呈现嵌套注释树。基本上,父注释可以有子注释,每个子注释可以有自己的子注释 每个评论也有一个回复字段,并向我的后端API发出post请求 我的指令代码:Javascript 角度:如何更新递归指令范围/模型,javascript,angularjs,directive,Javascript,Angularjs,Directive,我使用递归指令呈现嵌套注释树。基本上,父注释可以有子注释,每个子注释可以有自己的子注释 每个评论也有一个回复字段,并向我的后端API发出post请求 我的指令代码: commentTree.directive("tree", function($compile) { return { restrict: "E", scope: {parent: '=', onReply: '&'}, template: '<ul>' +
commentTree.directive("tree", function($compile) {
return {
restrict: "E",
scope: {parent: '=', onReply: '&'},
template:
'<ul>' +
'<li ng-repeat="comment in parent">' +
'<p>{{ comment.body }}</p>' +
'<label>by {{ comment.submitter }}</label><label>@ {{ comment.created_at | date : "MM/dd @ h:mma" }}</label>' +
'<form>' +
'<label>Comment: </label><textarea ng-model="commentBody"></textarea>' +
'<button ng-click="onReply({comment : comment, commentBody: commentBody})" class="btn btn-default">Reply</button>' +
'</form>' +
'<tree parent="comment.comments" on-reply="onReply({comment : comment, commentBody: commentBody})"></tree>' +
'</li>' +
'</ul>',
compile: function(tElement, tAttr, transclude) {
var contents = tElement.contents().remove();
var compiledContents;
return function(scope, iElement, iAttr) {
if(!compiledContents) {
compiledContents = $compile(contents, transclude);
}
compiledContents(scope, function(clone, scope) {
iElement.append(clone);
});
};
}
};
});
我猜这不起作用,因为指令不知道如何连接它?不太确定,如果有任何提示,我们将不胜感激
澄清一下:我用HTML调用我的指令,如下所示:
$http.post("/api/comments", { comment: { body: comment , story_id: $routeParams.id }}).
success(function(createdComment) {
$scope.allComments.push(createdComment);
}
)