Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check type of variable in HTML

Tags:

html

angular

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>
like image 209
Raza Ellahi Avatar asked Dec 15 '18 09:12

Raza Ellahi


People also ask

How do you check the type of a variable in HTML?

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.

How do you check the type of a variable in a script?

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.

What operator do we use to check the type of variable?

Use the typeof operator for detecting types.

How to check data type in JavaScript?

You can use the JavaScript typeof operator to find the type of a JavaScript variable.


1 Answers

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.

like image 119
Fateme Fazli Avatar answered Oct 02 '22 13:10

Fateme Fazli