首页 > 解决方案 > AngularJS ng-bind-html 不适用于表达式

问题描述

我正在尝试评估 ng-bind-html 中的表达式,但它不起作用。如果有人知道解决方法,请提供帮助。

下面的代码没有显示任何内容。

<h5 ng-bind-html="{{feedType ==='popularProducts' ? displayedProduct.title : displayedProduct.name}}"></h5>

标签: javascripthtmlangularjs

解决方案


ngBindHtml指令期望属性值作为表达式,因此不需要{{}}插值。只需删除这些大括号,以便将表达式直接传递给指令。

工作语法:

<h5 ng-bind-html="feedType === 'popularProducts' ? displayedProduct.title : displayedProduct.name"></h5>

适用的 AngularJS文档


推荐阅读