2017-03-10 40 views
0

我有超過50個項目的列表。我只想顯示前10個項目,我會看到一個按鈕,當點擊後顯示下10個項目,並再次點擊時,將顯示下10個項目,直到全部顯示。在ng2中顯示更多按鈕angular2

<ul class="results-main-content"> 
 
    <li class="right-results-section"> 
 
    <ul class="_result-list"> 
 
     <li class="result" *ngFor="let searchResult of searchResults"> 
 
     {{searchResult.name}} 
 
     </li> 
 
    </ul> 
 
    </li> 
 
    <li class="showmore"> 
 
    <button class="show-more"> 
 
     <img class="more" src="_arrow-down.svg" alt="" /> 
 
    </button> 
 
    </li> 
 
</ul>

這是可能的angular2實現?

如果是這樣,請賜教和SO社區。

感謝

+0

的Dup的http://stackoverflow.com/questions/42458664/how-to-show-1-element-in-ngfor -in-angular2 –

回答

3

你應該使用下面的代碼

import {Component, NgModule} from '@angular/core' 
import {BrowserModule} from '@angular/platform-browser' 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <div> 
     <h2>Hello {{name}}</h2> 
     <ul class="results-main-content"> 
    <li class="right-results-section"> 
    <ul class="_result-list"> 
     <li class="result" *ngFor="let item of content"> 
     {{item.colorName}} 
     </li> 
    </ul> 
    </li> 
    <li class="showmore"> 
    <button class="show-more" (click)="getData()" [disabled]="counter>=content.length"> 
     Show more 
    </button> 
    </li> 
</ul> 
    </div> 
    `, 
}) 
export class App { 
    name:string; 
    data = [...]; // refer plunker 
    content:any[]=new Array(); 
    counter:number; 
    constructor() { 
    this.counter=0; 
    this.getData(); 
    this.name = 'Angular2' 
    } 
    getData(){ 
    console.log(this.counter + 'dat size'+this.data.length) 

    for(let i=this.counter+1;i<this.data.length;i++) 
    { 
    this.content.push(this.data[i]); 
    if(i%10==0) break; 
    } 
    this.counter+=10; 

    } 
} 

@NgModule({ 
    imports: [ BrowserModule ], 
    declarations: [ App ], 
    bootstrap: [ App ] 
}) 
export class AppModule {} 

LIVE DEMO

+0

我如何確保它僅在點擊時顯示10個項目? – DingDong

+0

查看for循環中的if條件 – Aravind

+0

我相信它不是for循環中的條件。它是我們在'[diabled]'處理程序中分配的條件。 – DingDong

相關問題