最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular ternary operator with html output - Stack Overflow

programmeradmin2浏览0评论

Is it possible to render the output from a ternary operator as html?

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

Also tried to modify a inline if/else directive from a previous question but can't get this to work either.

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

Is it possible to render the output from a ternary operator as html?

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

Also tried to modify a inline if/else directive from a previous question but can't get this to work either.

.filter('iif', ['$sce', function ($sce) {
    return function (input, trueValue, falseValue) {
        return $sce.trustAs('html', input ? trueValue : falseValue);
    }
}])
Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Jul 19, 2016 at 9:05 stigstig 1,2406 gold badges26 silver badges45 bronze badges 3
  • Yes. it is possible to render. Have you written ng-bind-html in your html? – Varit J Patel Commented Jul 19, 2016 at 9:07
  • Which angular version are you using? – sabithpocker Commented Jul 19, 2016 at 9:12
  • why don't you do all this logic inside your controller ? – Rathma Commented Jul 19, 2016 at 9:43
Add a ment  | 

1 Answer 1

Reset to default 5

Yes, Use ng-bind-html like this :

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

Replace values according to your requirements.

发布评论

评论列表(0)

  1. 暂无评论