Angular 2:根据下拉值显示div

时间:2017-04-23 06:45:58

标签: html angular

我有一个下拉菜单,根据值的选择,应显示相应的div。

我尝试过以下代码。但它没有用。

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>

任何帮助都会很棒。

谢谢。

1 个答案:

答案 0 :(得分:5)

使用 ===

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

<强> DEMO