【问题标题】:angularjs simple conditional [duplicate]angularjs简单条件[重复]
【发布时间】:2013-06-23 02:07:47
【问题描述】:

我知道我可以使用 ng-if 来做这样的条件:

  td.icon(ng-if="isAuthor()", colspan="2")
  td.icon(ng-if="!isAuthor()", colspan="3")

但是对于简单的事情来说似乎有点过于罗嗦了。有没有办法:

  td.icon(ng-if="!isAuthor()", colspan="{{if isAuthor(): 2 else 3}}")

【问题讨论】:

  • Angular 在 1.1.5 - See this question 中添加了对三元运算符(这是您要问的)的支持
  • 这是什么语法? Angular 继续使用 HTML。喜欢<td colspan="{{isAuthor() ? 2 : 3}}">。我一定是错过了什么。

标签: javascript html angularjs pug


【解决方案1】:

你可以有一个功能来做到这一点!

td.icon(colspan="getColspan()")

在你的控制器中:

$scope.getColspan = function () {
    if (isAuthor()) {
        return 2;
    } else {
        return 3;
    }
};

【讨论】:

  • 表达式中没有内置条件评估,对吧?
猜你喜欢
  • 1970-01-01
  • 2017-05-23
  • 2012-05-09
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-01
  • 2014-06-28
相关资源
最近更新 更多