In my view class i am rendering some data, i am getting a variable and i want to check that if the type of variable is Boolean than i want to show a switch button for it, but if the type is number then i want to show a slider for it.
<div *ngFor="let attribute of agent.attributes; let i = index">
<div class="row">
<div class="col s2">
<mat-card>
<mat-card-header>
<mat-card-title>{{agent.attributes[i].name}}</mat-card-title>
</mat-card-header>
<mat-card-content>
<div class="center">{{agent.attributes[i].value}}</div>
<!-- for this value i want to check the type for it, if it is boolean then
a switch button should show and if type is number than a slider show be shown -->
</mat-card-content>
<mat-card-actions>
<button mat-button>SAVE</button>
</mat-card-actions>
</mat-card>
</div>
</div>
</div>
The type of the value assigned to a variable determines the type of the variable. The typeof operator in JavaScript allows you to determine the type of value or type of value that a variable contains . There is just one operand for the typeof operator (a unary operator), which takes one variable as input.
typeof is a JavaScript keyword that will return the type of a variable when you call it. You can use this to validate function parameters or check if variables are defined. There are other uses as well. The typeof operator is useful because it is an easy way to check the type of a variable in your code.
Use the typeof operator for detecting types.
You can use the JavaScript typeof operator to find the type of a JavaScript variable.
Write a simple typeOf
method to check the type:
ts:
typeOf(value) {
return typeof value;
}
And then use it in your template:
<div *ngIf="typeOf(var1) === 'boolean'">switch button</div>
<div *ngIf="typeOf(var1) === 'number'">slider</div>
Here's a Working Sample StackBlitz for your ref.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With