2017-02-27 4 views
0

私は配列を持っていますので、一度に1つのオブジェクトしか配列に表示しません。一度オブジェクトが表示されたら、そのアレイをボタンで循環させたいと思っています。配列を表示させることはできますが、一度に1つのオブジェクトを表示するしか方法がわかりません。ここまで私が持っているもののうち、plunkerです。配列角度2を循環する

この場合、*ngForを正しく使用しているかどうかはわかりません。助けてくれてありがとう!

+0

あなたは、単一の値を表示するには、ngForは必要ありません。必要なのは、表示するインデックスです:{{myArray [theDisplayedIndex]}}。ボタンを使用してDisplayIndexを増やすと、解決策が得られます。 –

+0

サードパーティのサイトではなく、質問自体の[mcve]にすべての関連コードを入力してください。 –

答えて

2
@Component({ 
    selector: 'my-app', 
    template: ` 
    <div> 
     <div>{{index}}</div> 
     <h2>{{item[index].title}}</h2> 
     <button (click)="Next()">next</button> 
    </div> 
    `, 
}) 
export class App { 
    public item = ITEM; 
    constructor() {} 
    index = 0; 

    Next(id){ 
    this.index ++; 
    if(this.index >= this.item.length) { 
     this.index = 0; 
    } 
    } 
} 

Plunker example

0
@Component({ 
    selector: 'my-app', 
    template: ` 
    <div> 
     <h2 *ngFor="let myItems of items">{{myItems.title}}</h2> 
     {{index}} 
     <button (click)="next()">next</button> 
    </div> 
    `, 
}) 
export class App { 
    public item = ITEM; 
    public index = 0; 

    public get items() { 
    return this.item.filter((item, index) => index <= this.index); 
    } 
    constructor() {} 

    public next(){ 
    this.index = Math.min(++this.index, this.item.length - 1); 
    } 
} 
関連する問題