简体   繁体   English

:带有* ngFor的第一个孩子无法正常工作

[英]:first-child with *ngFor is not working properly

i am trying to use :first-child on navigation bar buttons but it works on all nav's buttons from some reason. 我试图在导航栏按钮上使用:first-child ,但是由于某种原因,它可以在所有导航按钮上使用。 the same works with :last-child . :last-child

html: HTML:

<div fxLayout="row" class="top-padding-30">
  <ul fxFlex="100" *ngFor="let tab of navigation_buttons;">
    <li class="tab tab-text" [ngClass]="{'active-tab': tab.isSelected == true}" (click)="selectTab(tab)" fxLayoutAlign="center center">{{tab.name}}</li>
  </ul>
</div>

scss: SCSS:

.tab {
  background-color: #d2d2d2;
  padding: 18px;
  cursor: pointer;
  margin-right: 3px;
  &.active-tab {
    background-color: #452f46;
    color: #ffffff;
  }
  &:first-child {
    border-radius: 0px 15px 15px 0px;
  }
  //   &:last-child {
  //     border-radius: 15px 0px 0px 15px;
  //   }
}

ts: TS:

navigation_buttons = [{
  isSelected: false,
  name: 'לבנה (6%)'
}, {
  isSelected: false,
  name: 'גבינה לבנה (12%)'
}, {
  isSelected: false,
  name: 'גבינות צהובות (17%)'
}, {
  isSelected: false,
  name: 'גבינה בולגרית (25%)'
}, {
  isSelected: true,
  name: 'יוגורטים (60%)'
}]

This is happening because that all of .tab is actually the first-child of his parent (the ul ). 之所以发生这种情况,是因为所有.tab实际上都是其父母( ul )的第一个孩子。

You probably wanted to put the *ngFor on the li node so it will repeat for the menu items. 您可能希望将*ngFor放在li节点上,以便对菜单项重复此操作。

If not, please post your output html so we could see what's happening here. 如果没有,请发布您的输出html,以便我们可以看到这里发生了什么。

ngFor has a built-in "first" and "last" ngFor具有内置的“第一个”和“最后一个”

This is from the docs: 这是从文档:

<li *ngFor="let user of userObservable | async as users; index as i; first as isFirst">
   {{i}}/{{users.length}}. {{user}} <span *ngIf="isFirst">default</span>
</li>

Docs: https://angular.io/api/common/NgForOf#usage-notes 文件: https//angular.io/api/common/NgForOf#usage-notes

You can use first and last in conjunction with [ngClass] or [ngStyle] 您可以将first和last与[ngClass]或[ngStyle]结合使用

FYI first and last are just boolean values. 仅供参考,布尔值只是布尔值。

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

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