简体   繁体   English

绑定[!hidden]以选择Angular 2的盒子选项

[英]bind [!hidden] to select box option with Angular 2

I'm trying to add a show/hide behavior to a select box using Angular 2+ so basically I have : 我正在尝试使用Angular 2+将显示/隐藏行为添加到选择框,所以基本上我有:

 <select>
      <option disabled selected>Flow progres</option>
      <option *ngFor='let flow of flows'>{{flow}}</option>
    </select>

      <div [hidden]="true">
        <p>The flow progress is on going</p>
      </div>

and .ts: 和.ts:

export class ProductListComponent implements OnInit{
    flows = ["Passed",'Waiting','In Progres',' Failed'];
}

so when "in progress" option will be selected I want to show that hidden div, otherwise the div will be hidden for the other options. 因此,当“进行中”选项被选中时,我要显示该隐藏的div,否则其他选项的div将被隐藏。

Create a variable inside component. 在组件内部创建一个变量。

public showHide:boolean = false; //Set default value if you like else not

Add onChange event on select like: 在选择时添加onChange事件,例如:

<select #t (change)="handleSelectedValue(t.value)">
      <option disabled selected>Flow progres</option>
      <option *ngFor='let flow of flows'>{{flow}}</option>
    </select>

in component write the function like this: 在组件中编写如下函数:

handleSelectedValue(value) {
 // Get and value and assign it to variable declared above 
     if(value == 'In Progres')
    this.showHide = true;

}

in your html bind this showHide variable to Div like this: 在您的html中,将showHide变量绑定到Div像这样:

<div *ngIf="showHide">
        <p>The flow progress is on going</p>
</div>

Change your HTML the following way. 通过以下方式更改HTML。 Import all your dependencies from angular core module. 从角度核心模块导入所有依赖项。

<select [ngModel]="selected">
    <option disabled selected>Flow progres</option>
    <option *ngFor='let flow of flows'>{{flow}}</option>
</select>

<div [hidden]="selected!=='In Progress'">
    <p>The flow progress is on going</p>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM