Issue
`I have a problem on how to display a conditional value. Here is an example of the data whose elements I would like to display:
"dataList" : [
{
"title" : "Title 1",
"part" : "Part 1",
"num" : "7",
"code" : "yyAD_ikd@4&",
"rfa" : null,
},
{
"title" : "Title 2",
"part" : "Part 2",
"num" : "2",
"code" : null,
"rfa" : "m9*shuf#sd",
},
{
"title" : "Title 3",
"part" : "Part 3",
"num" : "2",
"code" : null,
"rfa" : "Z%usjd&&alp",
}
]
I need to loop to display the data in a table containing 3 columns (Title, Part and Info). The problem arises on the Info column which must contain the "code" if "num"=7 and the "rfa" if "num"=2. Knowing that if the code is present then the rfa is null, and vice versa.
I was able to do something like this but it doesn't work as expected (the title and part are well displayed but not the info) :
<table class="table table-bordered table-condensed table-body-center" ng-hide="true">
<tr>
<th>Title</th>
<th>Part</th>
<th>Info</th>
</tr>
<tr ng-repeat="res in dataList">
<td>{{res.title}}</td>
<td>{{res.part}}</td>
<td ng-show="res.code">{{res.code}}</td>
<td ng-show="res.rfa">{{res.rfa}}</td>
</tr>
</table>
The correct expected result in my example case should be the following :
Title | Part | Info |
---|---|---|
Title 1 | Part 1 | yyAD_ikd@4& |
Title 2 | Part 2 | m9*shuf#sd |
Title 3 | Part 3 | Z%usjd&&alp |
Solution
Just use the conditional operator something like
<table class="table table-bordered table-condensed table-body-center" >
<tr>
<th>Title</th>
<th>Part</th>
<th>Info</th>
</tr>
<tr ng-repeat="res in dataList">
<td>{{res.title}}</td>
<td>{{res.part}}</td>
<td>{{res.code== null ? res.rfa : res.code}}</td>
</tr>
</table>
Or
<td>{{res.code || res.rfa}}</td>
Answered By - jitender
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.