javascript - AngularJS:在 .post() 之后添加事件监听器

标签 javascript angularjs

我正在努力学习开发,如果我有点不了解,请见谅。我试着在这里搜索解决方案。 我必须发表评论并回复应用程序。我正在使用此功能向我的 View 添加评论。

$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');
});

我的问题是,当我添加新评论时,它没有关联此事件监听器。如何解决?

最佳答案

使用directivejQLite

HTML:

 <div comment-click> 
    //your comment html will goes here.
 </div>

JS:

app.directive("commentClick",function(){

    return {
        link : function(scope,ele,attr){

            ele.on('click',function(){

                //put here your commnet scropt

            })
        }
    }
})

将此 comment-click 属性添加到您要在其上绑定(bind)点击事件的新添加的 DOME 元素

关于javascript - AngularJS:在 .post() 之后添加事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37086834/

相关文章:

javascript - 带有 json 参数的 Angular http 请求

angularjs - AngularJS 中的增删改查

javascript - 从 tumblr 博客获取所有唯一标签

angularjs - 执行 PUT/POST 时没有 HTTP 资源错误 [CORS 问题 - AngularJS + Web API 2]

javascript - Angular $http promise 和回调未触发

javascript - 如何检查两个圆形div是否重叠

javascript - 打开新窗口,在该窗口中运行 $scope 函数,更好的方法

javascript - 是否可以将 C 代码编译为 Javascript 代码?

javascript - 在 JavaScript 中禁用 PrimeFaces selectOneMenu 组件

javascript - 在谷歌文档中,当您将一堆单元格传递到自定义函数中时,如何确定长度?