从指令中的ng-repeat内部更改范围

时间:2015-09-28 15:22:41

标签: javascript angularjs

我有这样的HTML:

<div dir-paginate="x in comments | itemsPerPage: commentsPerPage">
HERE IS DIRECTIVE FROM BLEOW
</div>

我需要更改commentsPerPage范围从自由...

这是指令,该指令在ng-repeat:

angular.module('commentsApp')
        .directive("addreply", addreply);



//ADD REPLY FUNCTION
function addreply(createCommentFactory)
{
    //CREATING BUTTON FOR REPLY AND CALL LINK FUNCTION
    var addReplys = {
        link: link,
        restrict: "EA",
        transclude: true
    };

    return addReplys;

    function link(scope, element, attrs)
    {
        scope.form = {
            reply: ""
        };



        //CALL FACTORY TO ADD NEW REPLAY ON CLICK
        element.bind('click', function () {
            scope.addReply = function (el) {
                createCommentFactory.saveComment(scope.form.reply[el.id], scope.project_id, el.id, el.level + 1)
                        .success(function (data) {
                            scope.commentsPerPage = scope.comments.length;
                        })
                        .error(function (data) {
                            console.log(data);
                        });
                // RESET REPLY TEXT AREA
                scope.form.reply = "";
            };

        });
    }
}

你看到我在scope.commentsPerPage = scope.comments.length;内尝试但没有工作......对此有什么解决方法吗?

1 个答案:

答案 0 :(得分:0)

您的指令的范围原型继承父母的范围。因此scope.commentsPerPage实际上是父scope.commentsPerPage的阴影版本。

作为快速解决方法,您可以使用$parent.commentsPerPage。作为一个更长期的解决方案,您应该重新构建代码以使用controller.property而不是scope.property语法。

您可以查看this article以获得深入的解释。