访问控制器中的ng-repeat变量

时间:2015-05-26 14:49:30

标签: javascript angularjs

以下是我目前的情况:

HTML

            <center><li ng-repeat = "x in items | orderBy: 'priority'"> 
            <!-- color code priorities -->
            <span ng-style="cmplt" ng-class="{ red: x.type == 'Work', blue: x.type == 'Personal' }">
                <b>{{ x.name }}</b></span>
            <span ng-class="{ yourChore: x.assignedTo == username} ">
                - {{ x.type }} (Priority {{ x.priority }}) Assigned by {{ x.creator }} to {{ x.assignedTo }}
            </span>&nbsp;&nbsp;

 <!-- When task is completed, user can click this button to mark it as such -->
            <button type="button" ng-click="cmplt={color: 'gray', 'text-decoration': 'line-through'} ; 
                markAs(this)">Completed</button>
            <div ng-class="{ red: x.completed == true }"> Hello</div>
            <button type="button" ng-click = "comment = true">Comment</button>
            <div ng-show="comment"><textarea rows="3" columns="50" ng-model="x.comments"></textarea></div>
            <div>{{ x.comments }}</div>
        </li></center>

的JavaScript

    $scope.markAs = function(repeatScope){
    if (!repeatScope.completed){
        repeatScope.completed = true;
    }
    else {
        repeatScope.completed = false;
    }
};

正在重复的对象中有一个布尔值,默认情况下标记为false,但单击按钮时应重新评估为true。问题是这不会发生(保持错误),我不确定为什么基于我的代码。

3 个答案:

答案 0 :(得分:3)

您可以按照ng-repeat

中定义的内容传入当前元素
<li ng-repeat="x in items"> <!-- reference to x within the repeat scope -->
    <button type="button" 
            ng-click="cmplt={color: 'gray', 'text-decoration': 'line-through'}; markAs(x)">
        Completed
    </button>
</li>

答案 1 :(得分:1)

您需要传递{ng}重复当前项目x,而不是this

<button type="button" ng-click="cmplt={color: 'gray', 'text-decoration': 'line-through'} ; 
            markAs(x)">Completed</button>

答案 2 :(得分:1)

解决方案很简单,尝试这样的事情:

使用时

ng-repeat = "x in items"

x将引用每个重复的项目。

markAs(x)

将在参数中使用“right”x启动该功能。