如何在ng-class中创建动态表达式

时间:2014-10-14 07:48:43

标签: angularjs

我有一个ngRepeat元素,其中包含一个触发确认消息的删除按钮。我正在尝试使用动态表达式来显示确认消息:

<div ng-repeat="item in items">
    <a ng-click="delete(item_id)"></a>
    <span ng-class="{'show':'delete_'+item._id}">Are you sure you want to delete your review? </span>
</div>

不幸的是,我无法将'delete_'+item._id表达式显示在ngClass指令中。有人可以提供解决方案吗?

2 个答案:

答案 0 :(得分:2)

只要表达式ng-class="{'show': 'delete_' + item._id}"评估为show(总是如此),使用'delete_' + item._id'就会添加类true。当用户点击show标记,即true时,您可能希望a旁边有一个ng-class="{'show': userWantsToDelete(item)}"的表达式。

此外,如果要将类'delete_' + item._id添加到元素,则可以在元素上使用带有class属性的角度表达式,即class="{{'delete_' + item._id}}"

答案 1 :(得分:0)

尝试这个。

<a ng-click="delete = !delete">delete</a>
<span ng-class="{show: delete}">Are you sure you want to delete your review? </span>

如果您要显示上次点击的<a>

的一条删除确认消息

在视图中

<a ng-click="delete(item._id)">delete</a>
<span ng-class="{show: item._id==delete_id}">Are you sure you want to delete your review? </span>

在控制器中

$scope.delete = function(id) {
    $scope.delete_id = ($scope.delete_id == id) ? !id : id;
}