Hasani
Hasani

Reputation: 3929

Why this switch statement doesn't work on Angular?

I have a switch statement like this:

  switch (selectedValue1 ){
    case selectedValue1 ==  'قطع از بالا':
      console.log(selectedValue1);
      break;

But I can not see the log message and meens the switch doesn't work! But I know and can see the selectedValue1 in log message and it works!

The selected value comes from this HTML code:

<div class="form-popup playerOne" id="myForm_if" dir="rtl">
    <form action="/action_page.php" class="form-container" >  
      <label for="funcs"><b>انتخاب شرط</b></label>

      <mat-select placeholder="انتخاب شرط " [(ngModel)] = "selectedValue1" name="condition" >
          <mat-option *ngFor="let ifin of ifs" [value]="ifin">
            {{ ifin }}
          </mat-option>
      </mat-select>
      <label for="funcs"><b>انتخاب نتیجه</b></label>

      <mat-select placeholder="انتخاب نتیجه" [(ngModel)] = "selectedValue2" name="result" >
          <mat-option *ngFor="let result of results" [value]="result">
            {{ result }}
          </mat-option>
      </mat-select>

        <button type="submit" class="btn " style="margin-top: 200px;" (click)="setCond(selectedValue1, selectedValue2)">اعمال</button>
        <button type="button" class="btn cancel" onclick="closeForm()">انصراف</button>
    </form>
</div>

And:

export const Ifs = ['انتخاب شرط', 'قطع از بالا', 'قطع از پایین'];
export const Results = ['انتخاب نتیجه', 'سیگنال خرید', 'سیگنال فروش', 'ارسال نتیجه' ];

Upvotes: 0

Views: 92

Answers (1)

Yehor Androsov
Yehor Androsov

Reputation: 6152

Switch-case automatically compares values, no need in ==

switch (selectedValue1 ){
    case 'قطع از بالا':
      console.log(selectedValue1);
      break;

When you do string comparison inside case you get this code snippet, which does not work, since selectedValue1 != true

switch (selectedValue1 ){
    case true: // or false
       console.log(selectedValue1);
       break;

Upvotes: 1

Related Questions