Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ng-attr-title using ternary operator in Angularjs?

Tags:

angularjs

I am very new to Angularjs. I want to set a value for title attribute, based on a boolean variable.

Sample code attached here.

 <tr ng-repeat="doc in $data" ng-class="{{doc.is_today}} ? 'highlight' : ''" 
                                        ng-attr-title="({{doc.is_today}}) ? 'Today' : ''"> 
     <td>1</td>
     <td>2</td>
     <td>3</td>
     <td>4</td>
     <td>5</td>
</tr>

Thanks,

like image 245
Deepak raj Avatar asked Sep 28 '22 00:09

Deepak raj


1 Answers

Remove {{}} in condition

<tr ng-repeat="doc in $data" ng-class="doc.is_today ? 'highlight' : ''" 
                                        ng-attr-title="doc.is_today ? 'Today' : ''"> 
     <td>1</td>
     <td>2</td>
     <td>3</td>
     <td>4</td>
     <td>5</td>
</tr>

Only use {{}} if you want to print the value.

Alternatively you can do the following also

<tr ng-repeat="doc in $data" ng-class="{{doc.is_today ? 'highlight' : ''}}" 
                                            ng-attr-title="{{doc.is_today ? 'Today' : ''}}"> 
         <td>1</td>
         <td>2</td>
         <td>3</td>
         <td>4</td>
         <td>5</td>
    </tr>

Link to fiddle

like image 73
Aakash Avatar answered Oct 10 '22 12:10

Aakash