點擊一個按鈕(這是頁面的底部),我想要去一個特定的元素(在我的情況下,#navbar),這是在當前頁面,但我不知道該怎麼做。我已經嘗試了下面的代碼無濟於事。滾動到Angular 4當前頁面的某個元素
<nav class="navbar navbar-light bg-faded" id="navbar">
<a class="navbar-brand" href="#">
{{appTitle}}
</a>
<!-- rest of the nav link -->
</nav>
<!-- rest of the page content -->
<!-- bottom of the page -->
<button class="btn btn-outline-secondary" (click)="gotoTop()">Top</button>
在角度成分:
import { Router } from '@angular/router';
/* rest of the import statements */
export class MyComponent {
/* rest of the component code */
gotoTop(){
this.router.navigate([], { fragment: 'navbar' });
}
}
我真的很感激,如果有人幫了我一個解決方案,並解釋了爲什麼我的代碼沒有工作。
請注意,元素(導航欄)位於其他組件中。
如何用標記替換按鈕並放置錨點? – Vega
它不會達到我的目的。我想使用點擊事件。也希望遵循嚴格的角度慣例。 – asmmahmud
從什麼時候Angular剝奪了你的約定HTML?這是沒有道理的,但尊重你的選擇 – Vega