0

Im trying to disable a button on a condition whether an object is empty or not. In this case the object selectedTopics should be equal to {}

register.ts

ngOnInit() {
    this.topicRequestAnswer = {topics:[]};

    this.selectedTopics = {};  // <- setting equal to {} here
    this.requestTopics();
  }

register.html

<ion-content class="ion-padding">

  <form #form="ngForm" (ngSubmit)="register(form)">
    <ion-grid>
      <ion-row color="primary" class="ion-justify-content-center">
        <ion-col class="ion-align-self-center" size-md="6" size-lg="5" size-xs="12">
          <div class="ion-text-center">
            <h3>...</h3>
          </div>
          <div class="ion-padding">
            <ion-item>
              <ion-input [(ngModel)]="email" name="email" type="email" placeholder="E-Mail" ngModel required></ion-input>
            </ion-item>
            <ion-item>
              <ion-label>Choose a topic</ion-label>
              <ion-select [(ngModel)]="selectedTopics" name="topics" multiple="true" ngModel required>
                <ion-select-option *ngFor="let topic of topicRequestAnswer.topics" value="{{topic}}" class="ion-text-justify" ngModel required>{{topic}}</ion-select-option>
              </ion-select>
            </ion-item>
            <ion-item>
              <ion-input [(ngModel)]="name" name="username" type="username" placeholder="Username" ngModel required></ion-input>
            </ion-item>
            <ion-item>
              <ion-input [(ngModel)]="password" name="password" type="password" placeholder="Password" ngModel required></ion-input>
            </ion-item>
          </div>
          <div class="ion-padding">
            <ion-button size="large" type="submit" [disabled]="form.invalid || selectedTopics == {}" expand="block">Sign Up</ion-button>  // <- the [disabled] condition fails here
          </div>
        </ion-col>
      </ion-row>
    </ion-grid>
  </form>

The output im getting in my php script when I echo the empty $topics variable is

[object Object]

How do I need to change the condition in order to disable the button in case no topics were selected?

Huondui
  • 383
  • 1
  • 3
  • 12

1 Answers1

1

Your condition will be false even if this.selectedTopics = {};

since basic principle {} == {} also returns false, so will your condition in [disabled] = selectedTopics == {}

One way to check is:

Object.keys(this.selectedTopics).length === 0

To check in html:

(selectedTopics | json) == '{}'
Tejeshree
  • 942
  • 6
  • 9