0

I have two buttons, going and maybe:

  toggles: any[] = [
    { response: 'going' },
    { response: 'maybe' }
  ];

which I display like this:

          <ion-col col-12 style="min-height: 250px;">
                <img class="event-home-img" src="{{mainEventImage}}" />
            </ion-col>

            <ion-col *ngFor="let toggle of toggles" col-6 style="text-align: center;">
                <button (click)="response(toggle.response)" ion-button color="danger" [attr.outline]="guestResponse !== toggle.response ? true : null"
                    full>
                    {{toggle.response}}
                </button>
            </ion-col>

guestResponse returns going or maybe based on the user's response. What I need is to add outline to the element if guestResponse doesn't match toggle.response. Now it doesn't add outline to either element.

Edit *** Response function

public guestResponse: string;

ngOnInit() {
const responses = ['going', 'maybe'];

    for (const key in responses) {
      if (responses.hasOwnProperty(key)) {
        this.db.object(`/events/${this.id}/${responses[key]}/${this.uid}`).valueChanges()
          .takeUntil(this.ngUnsubscribe)
          .subscribe((response) => this.getCurrentResponse(response, responses[key]));
      }
    }
}

  getCurrentResponse(response, key) {
    if (response) {
      this.guestResponse = key;
    }
  }

  response(event: string) {
    console.log(event);
    if (this.uid) {
      this.db.object(`/users/${this.uid}`).valueChanges()
        .takeUntil(this.ngUnsubscribe)
        .subscribe(user => this.setResponse(user, event));
    }
  }
9
  • I would start debug by [attr.outline]="guestResponse !== toggle.response ? 1: 0" Maybe null will get the whole attr ommitted Commented Oct 25, 2017 at 6:28
  • 1
    @günter-zöchbauer , the issue is "Now it doesn't add outline to either element." , means something is wrong about consition, user knows how to add dynamic attribute. Commented Oct 25, 2017 at 6:39
  • @VivekDoshi thanks for the hint and sorry for not paying enough attention. Commented Oct 25, 2017 at 6:41
  • 1
    @GünterZöchbauer, it happens sometime :) , no problem . Commented Oct 25, 2017 at 6:42
  • @Ciprian , Will you please also post the response function? Commented Oct 25, 2017 at 6:58

1 Answer 1

1

Please add :

this.guestResponse = event; inside your response(event: string) { ... } function.


response(event: string) {
    console.log(event);

    // add here if you want to show directly on click 
    // this.guestResponse = event; 

    if (this.uid) {
      this.db.object(`/users/${this.uid}`).valueChanges()
        .takeUntil(this.ngUnsubscribe)
        .subscribe(user => { 
            this.setResponse(user, event)
            // add here if you want after some db operation 
            // this.guestResponse = event;  
        });
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Still nothing. Maybe I made some other change which doesn't allow this to happen. I'll check. Thank you for your help.
You are right. Only thing I had to change is attr.outline to outline. forum.ionicframework.com/t/conditional-button-attributes/44743/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.