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

1
  • 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. Commented Aug 17, 2018 at 19:34

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

then how can select different title attribute? @Jonathan
Sorry my bad, I have update the code. It should work. You can try also with ng-attr-title
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.