4

I have a dropdown menu and based on the selection of the value , respective div should be shown.

I have tried the following code. but it doesn't work.

export class PropertyComponent {
regTypeSelectedOption: string = "";
selectedNav: any; navs = ['option 1', 'option 2', 'option 3']

ngOnInit() {
    this.selectedNav = 'select value';
}
setNav(nav:any){
        this.selectedNav = nav;
            if(this.selectedNav == "option 1"){
                this.regTypeSelectedOption = "option 1";
            }
            else if(this.selectedNav == "option 2"){
                this.regTypeSelectedOption = "option 2";
            }
            else if(this.selectedNav == "option 3"){
                this.regTypeSelectedOption = "option 3";
            }
        }

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.0/angular2.js"></script>
<div class="common_select_box">
<div class="input-group-btn dropdown {{dropdown1? 'open':''}}" 
(click)="dropdown1=!dropdown1">
    <button type="button" class="btn btn-default font_size_12 no_box_shadow 
ticket_info_subline_text_color" dropdown-toggle=""
        aria-haspopup="true" aria-expanded="false">{{selectedNav}}<span 
class="caret" style="color: #bfbfbf;"></span> </button>
<ul class="dropdown-menu">
 <li (click)="setNav(item)" *ngFor="let item of navs">{{item}}</li>
  </ul>

  <div *ngIf="regTypeSelectedOption == 'option 1'">
  option 1 
  </div>

  <div *ngIf="regTypeSelectedOption == 'option 2'">
  option 2 
  </div>

  <div *ngIf="regTypeSelectedOption == 'option 1'">
  option 3 
  </div>

Any help would be great.

Thank You.

5
  • Why did you import AngularJS 1.2.3 when you are using Angular 2? Commented Apr 23, 2017 at 6:50
  • What is the error that you are getting? Commented Apr 23, 2017 at 6:56
  • no error i get. Commented Apr 23, 2017 at 6:58
  • Then what is the issue? Commented Apr 23, 2017 at 6:58
  • when i run then i don't get div that is to be shown. Commented Apr 23, 2017 at 7:07

1 Answer 1

5

Use ===

  <div *ngIf="regTypeSelectedOption === 'option 1'">
  option 1 
  </div>

DEMO

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

1 Comment

Your Answer

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