我正在尝试学习开发,所以如果我有点不知情,很抱歉。我试着在这里搜索解决方案。我必须做一个评论和回复应用程序。我正在使用此函数向我的视图添加注释。
$scope.insertComment = function(){
var new_comment_id = $scope.comments[$scope.comments.length - 1].commentID +1;
var input = {
"commentID" : new_comment_id,
"userID": user.user_id,
"name": user.user_name,
"avatar_url": user.avatar,
"says": $scope.new_comment,
"likes": 0,
"like_status": false,
"replies": []
};
//Pushes changes to the global comment object
$scope.comments.push(input);
$scope.new_comment = '';
}
每条评论都有回复选项,下面的事件显示了一个表单来添加回复到评论。
$(".no-url").click(function(){
console.log('test2');
$(this).parent().children('.reply-form').addClass('active');
});
我的问题是,当我添加一个新的评论时,它并没有关联到这个事件监听器。如何解决这个问题?
发布于 2016-05-07 09:36:58
$(SELECTOR)
将只选择那些存在于DOM中的元素。
可以使用Event-Delegation
$(PARENT_SELECTOR).on('click', '.no-url', function() {
$(this).parent().children('.reply-form').addClass('active');
});
但更好的和推荐的解决方案是使用ng-click
指令。
发布于 2016-05-07 09:40:01
发布于 2016-05-07 10:27:09
以下是简化的代码,如何在不涉及jQuery的情况下仅使用angularjs完成此操作
<div class="comment" ng-repeat="comment in comments track by $index>
<p>{{comment.text}}</p>
<button ng-click="comment.reply = true">reply to comment</button>
<form ng-if="comment.reply">
<textarea ng-model="reply"></textarea>
<button ng-click="submitReply()">submit reply</button>
</form>
</div>
https://stackoverflow.com/questions/37086834
复制