0

Have something like this but the issue is when I click on any options it is not getting selected and the name is not getting changed to the option name. Currently I am using a drop down button which is kind of odd. But I kind of ran out of options and hoping that you guys can help me out of this. I have already tried the traditional way of using the "select" tag but the issue I am facing is that I need to customise the dropdown body which I am not able to do. The .html snippet is like this,

              <div class="month-selector btn-group">
                <button type="button" data-toggle="dropdown">
                  Dropdown button
                </button>
                <ul class="dropdown-menu scrollable-menu" role="menu">
                  <li>
                    <div class="item">Select Category</div>
                  </li>
                  <hr class="style-hr">
                  <li>
                    <div class="item">General Inquiries</div>
                  </li>
                </ul>
              </div>

The .ts file is like this.

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'app-case-accordian',
  templateUrl: './case-accordian.component.html',
  styleUrls: ['./case-accordian.component.scss']
})
export class CaseAccordianComponent implements OnInit {
constructor(){}
  ngOnInit() {}
}

What changes do I need to fo in .ts file so as to make the selected value reflect back in place of "Dropdown button". Any help is appreciated since I am new to Angular 6. Thanks in advance.

4
  • so when you click an element from the list you want the change be reflected on "Dropdown button"? IE: Click on "Select category" and then the text "Dropdown button" change in "Select category"? Commented Oct 15, 2018 at 9:52
  • yes exactly I need to do the same. Commented Oct 15, 2018 at 9:53
  • I've posted an answer. Is called double binding. Commented Oct 15, 2018 at 9:57
  • What if I want to use *ngFor in the future then how should I use it and how to use [(ngModel)]? Commented Oct 15, 2018 at 9:59

1 Answer 1

1

Edit your component.ts like this:

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'app-case-accordian',
  templateUrl: './case-accordian.component.html',
  styleUrls: ['./case-accordian.component.scss']
})
export class CaseAccordianComponent implements OnInit {
customText = "Dropdown button";
constructor(){}
  ngOnInit() {}

valueClick(value: string){
  this.customText = value;
}
}

then your html

   <div class="month-selector btn-group">
                <button type="button" data-toggle="dropdown">
                  {{customText}}
                </button>
                <ul class="dropdown-menu scrollable-menu" role="menu">
                  <li>
                    <div class="item" (click)="valueClick('Select Category')">Select Category</div>
                  </li>
                  <hr class="style-hr">
                  <li>
                    <div class="item"  (click)="valueClick('General Inquiries')">General Inquiries</div>
                  </li>
                </ul>
              </div>
Sign up to request clarification or add additional context in comments.

1 Comment

how can I implement the same by giving it to a parent div?

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.