1

In my angular app, I want to display a value:

<div>{{myValue}}</div>

I also want to make it display "Empty" if the value is empty, like so:

<div>{{myValue || 'Empty'}}</div>

How can I make myValue be formatted normally, but 'Empty' be italicized?

1
  • 1
    need to use ng-bind-html directive if you want to add HTML to the output Commented Feb 5, 2014 at 17:34

3 Answers 3

1

One way would be to use an ng-if to handle the two cases:

HTML Template:

<div ng-if="myValue">{{myValue}}</div>
<div ng-if="!myValue"><i>Empty</i></div>  <!-- css here! -->

However, this can be a bit verbose. If you have this occurring again and again, use a filter or bind ng-bind-html from a controller function.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use ng-show/ng-hide:

<div ng-show="myValue">{{myValue}}</div>
<div ng-hide="myValue"><i>Empty</i></div>

These directives take "truthy" values and shows/hides according to the passed value.

Comments

0

One angular way , is to use ng-bind-html

  <body ng-controller="ngBindHtmlCtrl">
    <p ng-bind-html="myValue"></p>
  </body>

Within controller, you manage emptyness condition:

app.controller('ngBindHtmlCtrl', ['$scope', function ngBindHtmlCtrl($scope) {
  if($scope.myValue == ''){
    $scope.myValue = '<i>Empty</i>';
  }
}]);

Check this plunker

HTH

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.