0

I have an Angular project that uses a dropdown from bootsnipp (Bootstrap snippets). The problem I'm having is that the dropdown won't work with the search bar.

//HTML

<div class="input-group">
  <div class="input-group-btn search-panel">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
      <span id="search_concept">Search In</span> <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu">
      <li><a href="#"><i class="fa fa-th" aria-hidden="true"></i> All Category</a></li>
      <li><a href="#"><i class="fa fa-laptop" aria-hidden="true"></i> IT Solutions</a></li>
      <li><a href="#"><i class="fa fa-paint-brush" aria-hidden="true"></i> Design and Multimedia</a></li>
      <li><a href="#"><i class="fa fa-globe" aria-hidden="true"></i> Social Media and Marketing</a></li>
    </ul>
  </div>
  <input type="hidden" name="search_param" value="all" id="search_param">         
  <input type="text" class="form-control" name="x" placeholder="Enter search keyword here">
  <span class="input-group-btn">
    <button class="btn btn-default" type="button"><span class="glyphicon glyphicon-search"></span></button>
  </span>
</div>

//JS

ngAfterViewInit(){
  $('.search-panel .dropdown-menu').find('a').click(function(e) {
    e.preventDefault();
    var param = $(this).attr("href").replace("#","");
    var concept = $(this).text();
    $('.search-panel span#search_concept').text(concept);
    $('.input-group #search_param').val(param);
  });
}
3
  • is there any javascript error in the console? Commented Dec 22, 2017 at 6:09
  • have you included bootstrap and jquery in angularcli.json file Commented Dec 22, 2017 at 6:41
  • How are you capturing the click event? Can you check that? Commented Dec 22, 2017 at 6:55

2 Answers 2

2

Please try to avoid jQuery in Angular. Angular have on way to do these tasks. First you need to understand how dropdown works in bootstrap. When you click on dropdown button or link it just add a class on your dropdown to show menu and the class name is "show". Now you know how it works. Now it's time to implement. Follow following steps to done your task.

  1. Declare a toggleFlag named variable in your component.

    public toggleFlag = false;

  2. Now create a click event on your dropdown menu button. This will call a method in your component.

    Search In

  3. Now create show method in your component. This method will chnage toggleFlag variable.

    public showDropdown() { this.toggleFlag = !this.toggleFlag; }

  4. Now it's time to use toggleFlag varibale in your template.

    ul class="dropdown-menu" role="menu" [ngClass]="{'show': toggleFlag}"

Hope it will help

Sign up to request clarification or add additional context in comments.

Comments

0

You can try with this ngx-bootstrap .

https://valor-software.com/ngx-bootstrap/#/dropdowns

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.