【问题标题】:Angular ternary operator with html output带有 html 输出的角三元运算符
【发布时间】:2016-07-19 09:05:52
【问题描述】:

是否可以将三元运算符的输出呈现为 html?

{{ boolA ? 'Some text' : '<i class=\"fa fa-check\"></i>' }}

还尝试从 previous question 修改内联 if/else 指令,但也无法使其正常工作。

.filter('iif', ['$sce', function ($sce) {
    return function (input, trueValue, falseValue) {
        return $sce.trustAs('html', input ? trueValue : falseValue);
    }
}])

【问题讨论】:

  • 是的。可以渲染。你在 html 中写过ng-bind-html 吗?
  • 您使用的是哪个 Angular 版本?
  • 你为什么不在你的控制器里做所有这些逻辑呢?

标签: javascript angularjs


【解决方案1】:

是的,像这样使用 ng-bind-html:

<span ng-bind-html=" your_condition ? 'html_if_true' : 'html_if_false' "></span>

根据您的要求替换值。

【讨论】:

  • 完美,谢谢。然后我也可以使用尖括号并转义引号。
猜你喜欢
  • 2023-02-03
  • 2015-05-03
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 2014-02-19
  • 2016-02-12
相关资源
最近更新 更多