0

Lets assume below sudo code, As you professionals can see I want to use Ternary kind of condition with ng-if to apply different HTML title attribute. I tried it as below but it did not work

<td>
     <span ng-if="isOk == true ? title ="Is Ok" : title ="Is Not Ok"></span>
</td>

I know I can achieve what I want applying ng-if in <td> level using below code

<td ng-if="isOk == true">
      <span title ="Is Ok"></span>
</td>
<td ng-if="isOk != true">
      <span title ="Is Not Ok"></span>
</td>

But I want to know weather I can use less code like Ternary check and achieve what I want with ng-if?

I thank you professionals in advace

  • I don't think it's possible with ngIf. As it specified on AngularJS website: "The ngIf directive removes or recreates a portion of the DOM tree based on an {expression}." A better approach could be writing a new directive implementing this behavior. – Jonathan Anctil Aug 17 '18 at 19:34

1 Answers1

1

As it was specified in this thread: if else statement in AngularJS templates, you can use ternary condition this way:

<span title="{{isOk ? 'Is Ok' : 'Is Not Ok'}}"></span>
Jonathan Anctil
  • 1,025
  • 3
  • 20
  • 44
  • then how can select different title attribute? @Jonathan –  Aug 17 '18 at 19:41
  • Sorry my bad, I have update the code. It should work. You can try also with ng-attr-title – Jonathan Anctil Aug 17 '18 at 19:46
  • Sorry Jonathan to ask u this question again, above code worked but what is instead of plain text like 'Is Ok' or 'Is Not Ok' we want to display text from angularJS model using {{text}} experssion? –  Aug 17 '18 at 20:07