简体   繁体   English

AngularJS在ng-repeat内单击不起作用

[英]Angularjs ng-click inside ng-repeat not working

I am working on a proof of concept where I am using angularjs. 我正在使用angularjs进行概念验证。 I have to use a click function on the links for which I am using ng-click. 我必须在使用ng-click的链接上使用click功能。 This ng-click is inside ng-repeat. 此ng-click位于ng-repeat内部。 I went through so many solved problems here in stackoverflow on how to use ng-click inside ng-repeat. 我在这里讨论了如何在ng-repeat中使用ng-click的许多已解决问题。 But somehow, none of the solutions worked. 但是不知何故,所有解决方案都没有用。 Can some one help me to point out where I am doing the mistake? 有人可以帮我指出我在哪里做错吗?

HTML Code :

<collection collection='tasks'></collection>
<collection collection='articleContent'></collection>   

app.directive('collection', function () {
    return {
        restrict: "E",
        replace: true,
        scope: {
            collection: '='
        },
        template: "<ul><member ng-repeat='member in collection' member='member'></member></ul>"
    }
});

app.directive('member', function ($compile) {
    return {
        restrict: "E",
        replace: true,
        scope: {
            member: '='
        },
        template: "<li><a href='#' ng-click='getArticleContent(member.itemId)'>{{member.title}}</a></li>",
        link: function (scope, element, attrs) {
            if (angular.isArray(scope.member.tocItem)) {
                if(scope.member.hasChildren == "true")
                    {
                    for(var i=0;i<scope.member.tocItem.length;i++){
                        if(scope.member.tocItem.title) {
                         scope.member.tocItem.title.hide = true;
                        }
                        }
                    }
                element.append("<collection collection='member.tocItem'></collection>"); 
                $compile(element.contents())(scope)
            }
        }
    }
});

app.controller('AbnTestController', function($scope) {
    var bookId ="";
    $scope.tasks = data;

    $scope.getArticleContent = function(itemId){
        alert('inside article content');
        $scope.articleContent = articleData[0].articleContent;
    }
});

on click of the link, getArticleContent method is never called here. 单击链接后,此处永远不会调用getArticleContent方法。

Directive member has it's own scope, meaning isolated scope. 指令成员具有自己的范围,即孤立的范围。 And the template has an ng-click to execute getArticleContent, that the member directive does not contain the getArticleContent function. 并且模板具有ng-click来执行getArticleContent,该成员指令不包含getArticleContent函数。

You have two choices: 您有两种选择:

  1. Add the getArticleContent function to the directive member. 将getArticleContent函数添加到指令成员。
  2. Create member directive without isolated scope. 创建没有隔离范围的成员指令。 There are issues with this though, having two instances of the same directive may cause conflicts. 但是,这存在一些问题,具有相同指令的两个实例可能会导致冲突。

Updated after OP comment: OP评论后更新:

I'm adding OP code with some data passed to directives for manipulation: 我正在添加OP代码,并将一些数据传递给指令进行操作:

app.directive('collection', function() {
    return {
        restrict: "E",
        replace: true,
        scope: {
            collection: '=',
            articleData: '=',
            articleContent: '='
        },
        template: "<ul><member ng-repeat='member in collection' member='member' article-data='articleData' article-content='articleContent'></member></ul>"
    }
});

app.directive('member', function($compile) {
    return {
        restrict: "E",
        replace: true,
        scope: {
            member: '=',
            articleData: '=',
            articleContent: '='
        },
        template: "<li><a href='#' ng-click='getArticleContent(member.itemId)'>{{member.title}}</a></li>",
        link: function(scope, element, attrs) {

            scope.getArticleContent = function(itemId) {
                alert('inside article content');
                scope.articleContent = articleData[0].articleContent;
            }

            if (angular.isArray(scope.member.tocItem)) {
                if (scope.member.hasChildren == "true") {
                    for (var i = 0; i < scope.member.tocItem.length; i++) {
                        if (scope.member.tocItem.title) {
                            scope.member.tocItem.title.hide = true;
                        }
                    }
                }
                element.append("<collection collection='member.tocItem'></collection>");
                $compile(element.contents())(scope)
            }
        }
    }
});

app.controller('AbnTestController', function($scope) {
    var bookId = "";
    $scope.tasks = data;

    $scope.getArticleContent = function(itemId) {
        alert('inside article content');
        $scope.articleContent = articleData[0].articleContent;
    }
});

It would be more helpful if OP can create a jsfiddle for us to review and revise. 如果OP可以为我们创建一个jsfiddle以便我们进行审查和修改,则将更加有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM