ng-if在ng-repeat内总是返回true

时间:2022-12-02 16:50:35

I am creating a table with data from the Premier League. I am trying to use ng-if to check if the home team won and if so add a cell saying "Home team won"

我正在创建一个包含英超联赛数据的表格。我正在尝试使用ng-if来检查主队是否赢了,如果是这样的话,添加一个单元格说“主队赢了”

HTML

HTML

<div ng-if="data.full_time_result: === 'H'">
    <td>Home Team Won!</td>
</div>

However this is adding the "Home Team Won!" to every row even when the full_time_result isn't 'H'.

然而,这增加了“主队赢了!”即使full_time_result不是'H',也是每一行。

Here is a link to the plnkr.

这是plnkr的链接。

Also, what would be the recommend way to achieve this functionality? Having lots of ng-if blocks probably isn't the best way to go about this.

此外,实现此功能的推荐方法是什么?有很多ng-if块可能不是解决这个问题的最好方法。

1 个解决方案

#1


5  

You can not have div inside table element tr directly, it would make an invalid html. To make it working you could wrap that div in td tag.

你不能直接在表元素tr中使用div,它会使得无效的html。为了使它工作,你可以在td标签中包装该div。

Markup

标记

<td>
    <div ng-if="data.full_time_result === 'H'">Home Team Won!</div>
</td>

Working Demo

工作演示

#1


5  

You can not have div inside table element tr directly, it would make an invalid html. To make it working you could wrap that div in td tag.

你不能直接在表元素tr中使用div,它会使得无效的html。为了使它工作,你可以在td标签中包装该div。

Markup

标记

<td>
    <div ng-if="data.full_time_result === 'H'">Home Team Won!</div>
</td>

Working Demo

工作演示