1

In my Angular template I have this bit of code:

  <div class = "btn-group" data-toggle="buttons">
    <input type="text" class = "form-control" [(ngModel)]="siteFilter[filterType]" placeholder="Refine Search">
    <label class="btn btn-default">
        <input type="radio" (change)="changeFilter('1')" autocomplete="off">1
    </label>
    <label class="btn btn-default active">
        <input type="radio" (change)="changeFilter('2')" autocomplete="off" checked>2
    </label>
    <label class="btn btn-default">
        <input type="radio" (change)="changeFilter('3')" autocomplete="off">3
    </label>
</div>

When I use the data-toggle="buttons" attribute, my (change)="changeFilter() is not even triggered. How do I keep the the data-toggle functionality and have the change event trigger on a radio press?

3
  • You may consider using ng-bootstrap (Angular 4 + Bootstrap 4). The NgbRadioGroup could give you the functionality that you are looking for. See the code examples. Commented Aug 7, 2017 at 22:31
  • Yes, in hindsight I should have used ng-bootstrap from the beginning, but I'd prefer to not have to convert the whole app now. Is there a way I can use ng-bootstrap without affecting the rest of my Bootstrap 3 elements? Commented Aug 7, 2017 at 22:40
  • I don't have a lot of experience with Bootstrap but I doubt that you can mix Bootstrap 3 (CSS + jQuery) with ng-bootstrap (Bootstrap 4 CSS, without jQuery). Mixing the two might be more complicated than converting to ng-bootstrap and upgrading to Bootstrap 4. Commented Aug 8, 2017 at 0:32

2 Answers 2

1

Try to to bind the click event on the label (it worked for me):

<label class="btn btn-default" (click)="changeFilter('1')">
    <input type="radio" autocomplete="off">1
</label>
Sign up to request clarification or add additional context in comments.

3 Comments

I believe I tried that. That normally works, but does it work with the data-toggle="buttons"?
Yes, I've something working with that; however I'm using bootstrap 4.
this worked for me after several hours of searching. bootstrap 3. thanks
0

What I ended up doing was have a flag variable for each of my inputs:

[ngClass]="nameFilterActive ? 'btn btn-default active': 'btn btn-default'

Then in my Component I had a function that would change the flags depending on what was pressed.

Comments

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.