Javascript 访问控制器中的ng repeat变量
以下是我目前拥有的: HTMLJavascript 访问控制器中的ng repeat变量,javascript,angularjs,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'
<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>
<!-- 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>
正在重复的对象中有一个布尔值,默认情况下该值被标记为false,但单击按钮时应重新计算为true。问题是这不会发生(保持为假),我不确定为什么基于我的代码。您需要传递
x
,它是ng repeat中的当前项,而不是此
<button type="button" ng-click="cmplt={color: 'gray', 'text-decoration': 'line-through'} ;
markAs(x)">Completed</button>
已完成
您需要传递x
,它是ng repeat中的当前项,而不是此
<button type="button" ng-click="cmplt={color: 'gray', 'text-decoration': 'line-through'} ;
markAs(x)">Completed</button>
已完成
解决方案非常简单,请尝试以下方法:
就像你以前一样
ng-repeat = "x in items"
x将引用每个重复项
markAs(x)
将使用参数中的“right”x启动函数。解决方案非常简单,请尝试以下方法: 就像你以前一样
ng-repeat = "x in items"
x将引用每个重复项
markAs(x)
将使用参数中的“right”x启动函数。您可以通过在
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>
完整的
您可以通过在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>
完整的
你试过通过markAs(x)吗?成功了,谢谢!然而,“这个”为什么不起作用呢?@devqon你能把它作为一个答案发布出来让OP接受吗?OP,我相信这是一个范围问题。angular的乐趣在于创建了如此多的作用域,因此有时很难确定在任何时候使用的是哪个作用域。重复ng时,将定义x范围。当使用“this”时,在单击之前不会对其进行评估。您尝试过传递markAs(x)吗?成功了,谢谢!然而,“这个”为什么不起作用呢?@devqon你能把它作为一个答案发布出来让OP接受吗?OP,我相信这是一个范围问题。angular的乐趣在于创建了如此多的作用域,因此有时很难确定在任何时候使用的是哪个作用域。重复ng时,将定义x范围。使用“this”时,在单击之前不会对其进行计算。