javascript - Angular 2 使用 *ngFor 和 ngIf 隐藏第一个元素

标签 javascript angular angular2-template angular2-directives

在 Angular 2 中,我得到了一个项目列表,并根据给定的条件(即基于位置编号)将列表设置为重复。

我需要为每个位置的第一个框隐藏“ 删除文本 ”。

普朗克:
https://plnkr.co/edit/xtX5BjTBjO61sD2tLwWN?p=preview :

actual img

[1]: https://plnkr.co/edit/xtX5BjTBjO61sD2tLwWN?p=preview

import {Component} from '@angular/core';

@Component({
	selector: 'app',
	template: `
	  
    <ul *ngFor="let locdata of locations">
   
     <template ngFor let-item [ngForOf]="items"; let-i=index>
          <div *ngIf="item.location==locdata.location">
             <div class="title"> location  {{ item.location}} <span  *ngIf="isNotFirst(item)">  remove </span> </div>
          <div class="container"> {{ item.sedule}}</div>
       </div>
      </template>
	  </ul>
	 
	`
})
export class App {
  items: string[];
  
   isNotFirst(item: any) {
      return this.items.filter(i => i.location === item.location).map(i => i.id).indexOf(item.id) !== 0;
    }
  
  locations:any;
  
  constructor() {
    this.locations=[{location:1},{location:2},{location:3}]; 
    
    this.items = [{
    id:1,
    location:1,
    sedule:1
  },
  {
    id:2,
    location:2,
    sedule:1
  },
  {
    id:3,
    location:1,
    sedule:2
  },
  {
    id:4,
    location:2,
    sedule:2
  },
  {
    id:5,
    location:1,
    sedule:2
  },
  {
    id:6,
    location:2,
    sedule:3
  },  {
    id:7,
    location:3,
    sedule:1
  },
  {
    id:8,
    location:3,
    sedule:2
  },
  {
    id:9,
    location:3,
    sedule:3
  }];
  }
  
}

最佳答案

假设您有一个 <span>*ngFor逻辑,你可以使用 first来自 *ngFor并显示/隐藏 <span>来自 *ngIf

<div *ngFor="let item in data; let first=first;">
  {{item.attr}}
  <span *ngIf="!first">
    remove
  </span>
</div>

您的工作 plunker .

关于javascript - Angular 2 使用 *ngFor 和 ngIf 隐藏第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44170366/

相关文章:

javascript - 获取键盘数字键中符号的javascript keycode

php - 用php获取内部html

javascript - Angular 4 : abort all pending $http requests on route change

javascript - 多次实例化@ViewChild,这样我就可以再次使用HTML Angular 4

css - 使 ng-select 宽度调整为所选元素/可用选项?

javascript - @Angular - 公共(public) block 在构建时获取延迟加载的内容

javascript - 有没有 ECMAScript Validator

javascript - 如果文本比容器长,如何使文本换行

angular - Response.json() 使用接口(interface)绑定(bind)到单个对象 - RxJs/Observable 失败

javascript - 按日期对 Trello 卡片数组进行排序