stibay stibay - 4 months ago 36
AngularJS Question

Angular ternary operator with html output

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);
}
}])

Answer

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.