[英]How to pre selected first tab always from the dynamic tabs?
I am working on an angular 4 project, I need to create a structure in which there are three checkboxes. 我正在做一个angular 4项目,我需要创建一个包含三个复选框的结构。 A tab should be dunamically created once the first checkbox is selected, so by that logic if I select all three of them then there will be three tabs, the problem I am stuck at it as follows:I need at any gievn point of time, one tab to be selected, so lets say if first checkbox is selected then first tab should be preselected, if first checkbox is unselected then second tab should be selected..any help this regard will be highly appreciated. 一旦选择了第一个复选框,就应该自动创建一个标签页,因此按照这种逻辑,如果我选择了所有三个标签页,则将有三个标签页,我遇到的问题如下:我需要在任何时间点,可以选择一个选项卡,因此可以说,如果选择了第一个复选框,则应预选择第一个选项卡,如果未选择第一个复选框,则应选择第二个选项卡。在这方面的任何帮助将不胜感激。 Thanks! 谢谢!
For better understanding I had created a plunker example: https://plnkr.co/edit/2UPBf67y2ExmLaePP3VV?p=preview 为了更好地理解,我创建了一个小例子: https ://plnkr.co/edit/2UPBf67y2ExmLaePP3VV ? p = preview
Html code: HTML代码:
<div *ngFor="let industry of industries">
<input type="checkbox" (change)="onIndustryChange($event.target.checked, industry.id)">
<span>{{industry?.name}}</span>
</div>
<div class="tab-wrapper">
<ul class="nav nav-tabs app-tab-menu">
<ng-container *ngFor="let industry of industries">
<li *ngIf="isIndustrySelected(industry.id)">
<a href="#industry_{{industry.id}}" data-toggle="tab">{{industry?.name}}</a>
</li>
</ng-container>
</ul>
<div class="tab-content">
<ng-container *ngFor="let industry of industries">
<div class="tab-pane" id="industry_{{industry.id}}" *ngIf="isIndustrySelected(industry.id)">
<span>{{industry.name}}</span>
</div>
</ng-container>
</div>
You need to check if the current index is equal to 0, then set the [checked]
property of the checkbox. 您需要检查当前索引是否等于0,然后设置复选框的[checked]
属性。
<div *ngFor="let industry of industries; let i = index">
<input type="checkbox" [checked]="i ==0" (change)="onIndustryChange($event.target.checked, industry.id)">
<span>{{industry?.name}}</span>
<ul class="nav nav-tabs app-tab-menu">
<ng-container *ngFor="let industry of industries; let i = index">
<li *ngIf="isIndustrySelected(industry.id) || i == 0">
<a href="#industry_{{industry.id}}" data-toggle="tab">{{industry?.name}}</a>
</li>
</ng-container>
</ul>
** UPDATE ** **更新**
Since this will not work based on your requirements, here is my suggestion: 由于这无法根据您的要求进行,因此,我的建议是:
industry
would come from an api call so it will return a promise. 如果我没看错, industry
将来自api调用,因此它将返回一个承诺。 Put this code on ngOnInit()
so the html will not render until the data arrived. 将此代码放在ngOnInit()
以便在数据到达之前不会呈现html。 Then on put the first index of industry to selectedIndustries. 然后将第一个行业指数添加到选定的行业。 On html, you just add [checked]="isIndustrySelected(industry.id)"
on your first div. 在html上,您只需在第一个div上添加[checked]="isIndustrySelected(industry.id)"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.