2017-02-21 11 views
3

* ngForで複数の要素が生成されたページをスクロールダウンした場合、要素をクリックすると、ルータ経由で別のページに移動します。 ボタンをクリックして前のページに戻ると、ページを出る前の位置に画面がスクロールされます。動的Angular 2ページの指定された位置にナビゲート

どうすればこの問題を解決できますか?

+0

[**#fragment **](https://angular.io/docs/ts/latest/api/router/index/NavigationExtras-interface.html)を見てください。それはあなたを助けるかもしれません。 – developer033

+0

ありがとう、私は断片化しようとしましたが、すべての要素が生成される前に、DOMにアクセスしようとします。 –

+0

この問題で新しい質問をすることがあります。誰かがあなたを助けることができる.. – developer033

答えて

1

のは、仮定しようListページngForループのようにあります。そして、

this.router.navigate(['group', this.noteService.groupName, 'edit', note.$key], 
    { queryParams: { i: index } }); // pass in additional index in ngFor loop 

EditページがngOnInitthis.noteIndexとしてインデックスを覚えている:

<div *ngFor="let note of (noteService.notes | async); let i = index" class="item" tabindex="0"> 
    <a (click)="edit(note, i, $event)"> 
    {{note.text}} 
    </a> 
</div> 

edit機能では、それは...?i=11のようなURLに追加のインデックスを提供します戻るとき:

this.router.navigate(['group', this.noteService.groupName], 
    { queryParams: { i: this.noteIndex } }); // to let List page focus this note 

その後ListページのngOnInitは、以下のことが可能です。

this.noteService.announcedCountNotes.subscribe(
    count => { 
    // noteService announces new count after it saves edit 
    // so by now, ngFor should be ready 
    if (idxToFocus) { // this.route.snapshot.queryParams['i']; 
     setTimeout(_ => { 
     var elements = document.querySelectorAll('div.item'); 
     var len = elements.length; 

     if (len > 0 && idxToFocus >= 0 && idxToFocus < len) { 
      const el = elements[idxToFocus] as HTMLElement; 
      //el.scrollIntoView(); 
      el.focus(); 
     } 
     }, 0); 
    } 
    } 
) 
あなたは div.item:focusのための特定のスタイルを提供する場合があります

は、この方法は私のために少なくともクロム59(Windows 7とiPadの)に取り組みました。

+0

私はquerySelectorAllを使ってHTMLElementsを入手するのが好きです – Jon

関連する問題