What is the best way to navigate to a specific component's location on

In Angular, I am looking to scroll to the position of a specific component. The components are created based on the repeticiones variable. Below is my HTML code for reference:https://i.sstatic.net/FJ7Do.png

Additionally, here is the TypeScript code associated with this task:https://i.sstatic.net/eCczE.png

Answer №1

I believe utilizing id and viewportScroller instead of elem would be more efficient

HTML

<div>
    <button (click)="onAnchorScroll(1)"></button>
    ...
</div>
<div *ngFor="let item of repecticiones; let i = index" [id]="i + 1">
    ...
</div>

TS

Don't forget to import ViewportScroller from @angular/common in your ts file

constructor(private viewportScroller: ViewportScroller) { }

onAnchorScroll(id) {
  this.viewportScroller.scrollToAnchor(id);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue encountered while attempting to set up auto-sizing for ion-textarea with a directive

I'm working on integrating an ion-textarea into my Ionic application. However, I've encountered an issue where I am unable to adjust the height of the textarea. After some research, I came across a solution for auto-sizing the textarea by creatin ...

Are multiple click events needed for identical buttons?

In my component, there is an HTML structure like this: <div id="catalogo" class="container"> <div class="row"> <div *ngFor="let artista of artistas" class="col-sm" style="mar ...

Encountered an issue when attempting to access a user's full details page in Angular 14

An error occurred in main.ts at line 6: TypeError - Cannot read properties of undefined (reading 'id'). The issue is located in the ContactUserDetailsComponent_Template in contact-user-details.component.html at line 17. This error is being hand ...

Enhance your angular application with universal support using the CLI for Angular 6

After creating a new Angular6 application using Angular CLI, I used the following command: ng generate universal This added support for Universal rendering. Everything is working fine, but I noticed that it also added the following code to my angular.jso ...

Avoid displaying the value in Ant Design's autocomplete feature

Can someone assist me with clearing the auto complete placeholder or displaying only part of the label instead of the value after a user selects from a drop-down list? The current setup shows the unique ID as the value, which we want to keep hidden from en ...

Angular 6: Addressing the 'ExpressionChangedAfterItHasBeenCheckedError' in dynamic form validation with reactive form by adding and removing elements

When utilizing Angular 6 Reactive Form and dynamically adding and removing new validators, an error occurs: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'disabled: false'. Current ...

finding the parent of form controls in Angular 2 requires understanding the hierarchical structure

Is there a way to access the parent form group of a nested form group in order to retrieve a sibling control value during validation? Both controls are within a formGroup that is part of a formArray. While I am aware of the root element, I am unsure how ...

An issue occurred with npm resulting in exit code 2. The error is as follows: Command was unsuccessful: node_modules/.bin/vsce package --no

Here is the specific error displayed in cmd: TS1323: Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'esnext', 'commonjs', 'amd', 'system', or 'umd'. E ...

Animating ng-bootstrap modals

It seems like the fade class is not working on the ngb-modal. I tried to create my own animation for the modal, but the ng-bootstrap injects the modal template into the dialog, making it difficult to access the dialogue itself. I only have access to the t ...

Angular: Resetting a conditional form control with matching name

Currently, my code looks something like this: <input *ngIf="a" #autocomplete formControl="myControl"> <input *ngIf="!a" #autocomplete formControl="myControl"> These inputs have different attributes depen ...

Is it possible to efficiently structure intricate JSON data onto interfaces in TypeScript with the incorporation of observables?

I am currently working on creating a simple web news reader that relies heavily on the Google News API (). I have set up all the necessary services, models, etc. However, I am having difficulty mapping the data onto specific interfaces. The Google API retu ...

What is the best way to retrieve an array stored within a key of a JSON object within an Angular service?

I have been learning about Angular heroes through a tutorial and decided to apply my knowledge in a small test project. While it worked fine with mock items using the built-in http server, I am facing difficulties retrieving items from a Django REST API. ...

Learn how to use the rendertron middleware in Node.js Express to redirect all routes to the original Angular app

Currently, I am working on creating a rendertron middleware using nodejs to determine when to utilize pre-rendered content and when to use the original application. However, I am facing challenges in redirecting to my usual Angular app using fetch or any o ...

Setting a default value in a formControl - a guide

I've been attempting to establish a default time value in an input field, but I found that the "value" attribute didn't work for me. <input class="form-control" name="absolute type="time" value="23:59" formCo ...

Generate md-card components in real-time using data fetched from an API

I have a scenario where I make an API call to fetch user profiles, and I want to generate Angular Material Design md-cards dynamically for each profile. The number of profiles retrieved can vary, hence the need for dynamic card creation. Below is the comp ...

Unexpected termination during the npm installation process without providing a clear error message

Every time I try to create a new Angular2 app using the command ng new new-app, angular-cli successfully generates the necessary files and then proceeds to run npm install. However, the npm install process abruptly stops with no error message provided. As ...

Tips for adjusting the color of sorting arrows in the Header of a Data table using Angular Material

I'm having trouble changing the header color of the sorting arrows in the Angular Material data table. You can view the data table here. Currently, the sorting arrows are gray by default and I want to change them to white. Despite my efforts, I haven ...

Encountered error in Angular unit testing: Route matching failed to find a match

I am currently working on unit testing in my Angular 15 application. While running the test, I encountered the following error: Error: Cannot match any routes. URL Segment: 'signin' Below is the code for the unit test of my component: fdescribe ...

What steps can I take to resolve the issues with these typescript errors?

An issue arises with type 'string' as it does not meet the constraint 'unknown[]': const query = db.prepareQuery<string>( TS2707 [ERROR]: The generic type 'RouterContext<R, P, S>' needs to have between 1 an ...

Extract the variable from the URL path

Looking to retrieve a variable from the URL using Angular. In my routes configuration, I have the following: const routes: Routes = [ { path: ':page', component: BlogComponent} ]; Additionally, in my blog component's constructor, I have ...