0

我正在尝试使用 ng-if 表达式将帖子呈现为图像或文本,具体取决于内容。这些行中的第一行在 ng-repeat-loop 中有时呈现 true 有时呈现 false ,但是图像和跨度都显示在每次迭代中。

<a href="{{post.url}}">
  {{post.type == 0}}
  <img ng-if="post.type == 0" src="{{post.content}}" />
   <span ng-if="post.type == 1">{{post.content}}<span>
</a>
4

3 回答 3

2

您将“post.type”的值定义为等于 0 或 1。使用“==”运算符确定该值是否等于比较

<a href="{{post.url}}">
  {{post.type = 0}}
  <img ng-if="post.type == 0" src="{{post.content}}" />
   <span ng-if="post.type == 1">{{post.content}}<span>
</a>

另一种方法:

{{post.type = 1}}
<a ng-if="post.type == 0" href="#"><img ng-if="post.type == 0" src="{{post.content}}" /></a>
<a ng-if="post.type == 1"><span>{{post.content}}</span></a>
于 2013-08-21T13:26:46.650 回答
1

尝试像这样使用它:

ng-if="post.type == '0'"

于 2014-05-20T21:30:18.253 回答
-2
  1. 有时在复杂的 HTML 中,不关闭标签会产生问题,并且您的 span 未关闭(否)。
  2. 只是为了测试用例尝试更改为 ng-show。
  3. JB Nizet 在评论中给了 ua 工作 plnkr,所以我认为在此之前有一个问题。
于 2013-08-21T13:54:14.433 回答