Angular Pipe for Handling Boolean Types
This post shows you how you can handle Boolean
types in an Angular
application using Pipe
. The Pipe
formerly known as Filter
in AngularJs
is an Angular feature that helps transform data. Example shows a very simple pipe that converts a boolean
value to a formatted string (yes or no).
Create a pipe without any hassle with the Angular CLI tool using the following command,
ng g pipe yesNo
Make the yesNo
pipe globally available so that you can use it in any component you like:
Use the pipe like the following while interpolation:
<div class="columns">
<div class="column is-half">
<table class="table is-fullwidth is-hoverable is-striped">
<thead>
<tr>
<th><abbr title="Team Name">Team</abbr></th>
<th><abbr title="Played">Pld</abbr></th>
<th><abbr title="Won">W</abbr></th>
<th><abbr title="Lost">L</abbr></th>
<th><abbr title="Points">Pts</abbr></th>
<th>Qualified for Semi Finals</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of data">
<td>{{ item.team }}</td>
<td>{{ item.played }}</td>
<td>{{ item.won }}</td>
<td>{{ item.lost }}</td>
<td>{{ item.points }}</td>
<td
[ngStyle]="{
color: item.isQualified
? 'hsl(141, 53%, 53%)'
: 'hsl(348, 100%, 61%)'
}"
>
<b>{{ item.isQualified | yesNo }}</b>
</td>
</tr>
</tbody>
</table>
</div>
<div class="column">
<pre class="user-pre">{{ structure | json }}</pre>
</div>
</div>
Here, data
is just a json
array field on your component. Here goes the full script of the component.
Repository:
https://github.com/fiyazbinhasan/ng-playground/tree/ng-playground-pipes
Links: