1

角2ではアイテムのリストを取得し、指定された条件(ロケーション番号に基づく)に基づいてリストを繰り返し設定します。角2で最初の要素を非表示にする* ngForとngIfで

各場所の最初のボックスに「テキストを削除する」を非表示にする必要があります。

Plunker: https://plnkr.co/edit/xtX5BjTBjO61sD2tLwWN?p=preview

actual img

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

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

 
@Component({ 
 
\t selector: 'app', 
 
\t template: ` 
 
\t 
 
    <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> 
 
\t </ul> 
 
\t 
 
\t ` 
 
}) 
 
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 
 
    }]; 
 
    } 
 
    
 
}

+0

私は 'first'要素にクラスを追加してCSSで隠すことができると思いますか? (参照:http://blog.angular-university.io/angular-2-ngfor/) – ochi

+0

私は得ることができないその論理をどのように追加しますか。 –

+0

私たちは[隠し]を使うこともできますが、[隠し] =条件は見つけることができません。 –

答えて

2

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

 
@Component({ 
 
\t selector: 'app', 
 
\t template: ` 
 
\t 
 
    <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> 
 
\t </ul> 
 
\t 
 
\t ` 
 
}) 
 
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 
 
    }]; 
 
    } 
 
    
 
}

+0

let-i =もうインデックス。 – Aman

+1

'template'はAngular4で非推奨です。 'ng-template'を使います。 – developer033

+1

このコードは質問に答えるかもしれませんが、どのようにして問題が解決されるのかについての追加の文脈を提供することで、回答の長期的価値が向上します。 –

4

あなたは、あなたが<span>*ngFor内のロジックを持っていると仮定します*ngForショーからfirstを使用することができます/ <span>

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

*ngIfすることにより、作業 plunkerを非表示にします。

+0

主な要件 –

+0

リンクを取得するplnkrコードを確認してください:https://plnkr.co/edit/lu37lt6Y9pe5dlOxXEZx?p = preview –

+0

@ B.V.SBharatKumarは私の答えでプランナーをチェックします。 – Pengyy

0

はこちらをご覧ください:

https://plnkr.co/edit/nMvnonrBjRfq1n8tmfZT?p=preview

お好きな条件を必要とする:あなたのテンプレートの一部の解読を行った後

*ngIf="i !== 1" 
+0

このplnkrをチェックすると、あなたは主なアイデアを得るでしょう –

+0

https://plnkr.co/edit/zUeh8o7UKY4DzWfFQdyP?p=preview –

+0

私たちは2つの場所を持っていません –

0

、私はあなたのアイテムのコレクション内の単一の項目のそれはあなたの基本的なHTMLと思います次のようになります。

@Component({ 
    selector: 'app', 
    template: ` 
     <h4>Syntax 1</h4> 
     <div> 
      <div class="title"> 
      location {{ item.location}} 
      <span *ngIf="true">remove</span> 
      </div> 
      <div class="container"> 
      {{ item.sedule}} 
      </div> 
     </div> 
    ` 
}) 
export class App { .... } 

icテンプレートを使用すると、* ngForこのHTMLの複数のバージョンをスタンプアウトすることができます。itemsコレクションの各アイテムに1つずつスタンプします。すなわち、最初の項目 - パズルの最後のピースは、あなただけのコレクション内の特定の項目についてremoveテキストを表示したいということです

@Component({ 
    selector: 'app', 
    template: ` 
     <h4>Syntax 1</h4> 
     <div *ngFor="let item of items"> 
      <div class="title"> 
      location {{ item.location}} 
      <span *ngIf="true">remove</span> 
      </div> 
      <div class="container"> 
      {{ item.sedule}} 
      </div> 
     </div> 
    ` 
}) 
export class App { .... } 

:あなたのように見えるテンプレートを与える

。ありがたいことに、ngForにはこの同じ問題の解決策があります。ngForにアイテムリストの現在のアイテムのインデックスを教えてもらうことができます。そのインデックスを使用して、「削除」テキストを表示または非表示にすることができます。最初の項目のインデックスは0であるため、ngIfテストはその値に対してテストします。あなたに与え

@Component({ 
    selector: 'app', 
    template: ` 
     <h4>Syntax 1</h4> 
    <ul> 
     <div *ngFor="let item of items; let i=index;"> 
      <div class="title"> 
      location {{ item.location}} 
      <span *ngIf="i != 0">remove</span> 
      </div> 
      <div class="container"> 
      {{ item.sedule}} 
      </div> 
     </div> 
    </ul> 
    ` 
}) 
export class App { .... } 

お知らせngFor文で変更を - let i=indexを使用することにより、次の方法で出力されているアイテムの現在のインデックスにマッピングされるローカルテンプレート変数iを作成していますngFor。次に、その値に対してテストして、必要に応じて要素を表示/非表示にすることができます。

ngForfirstlastevenとあなたもそれが最も広い到達用法を持っているので。私は、この例ではindexを使用を使用することができodd変数を提供し、indexに加えてなお、しかし、あなたはしましたができ簡単にこのユースケースのためにfirstを使用していました。

See the documentation for more info about ngFor

+0

各場所の削除を非表示にする必要があることを確認してくださいhttps://plnkr.co/edit/xtX5BjTBjO61sD2tLwWN?p=preview –

関連する問題