Oops, it looks like there's an issue - the scrollableDiv.nativeElement

I'm trying to use the scrollIntoView method on an HTML element within a component. However, I'm encountering the following error:

ERROR TypeError: "_this.scrollableDiv.nativeElement is undefined"

This is my Component Code:

 @Input("data") detailData : any = {};
@ViewChild('scrollableDiv') scrollableDiv: ElementRef;

ngOnInit() {
  console.log(this.detailData);
  const myNode = this.detailData;
  console.log(myNode)

  if (myNode) {
    console.log(this.detailData);
    setTimeout(() => {
      this.scrollableDiv.nativeElement.scrollIntoView({ behavior: "smooth", block: "start" });
    });

   }

}

The relevant section in the HTML looks like this:

<div [id]="detailData.key" #scrollableDiv>

I would appreciate any help or suggestions anyone can offer. Thank you in advance.

Answer №1

The ngOnInit function gets triggered before the view children are able to register.

It is recommended to use ngAfterViewInit() instead for better timing.

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

Encountered an issue while using OpenAPI 3.1 with openapi-generator-cli typescript-fetch. Error: JsonParseException - The token 'openapi' was not recognized, expected JSON String

I am interested in creating a TypeScript-fetch client using openapi-generator-cli. The specifications were produced by Stoplight following the OpenAPI 3.1 format. However, when I execute the command openapi-generator-cli generate -i resources/openapi/Attri ...

Using Rollup for TypeScript imports with absolute paths

Link to Source Code: https://github.com/arvigeus/roll-on-slow Output Bundle Location: dist Build Log: build.log After bundling with Rollup, warnings are thrown for incorrect source maps (Error when using sourcemap for reporting an error: Can't resolv ...

Does performing regForm.reset() not only clear the form but also deactivate it?

When I use .reset() to reset my form, it works as expected. However, after resetting, when I try to type in the input fields again nothing happens and they don't accept any inputs. <form (ngSubmit)="onFormSubmit(regForm)" #regForm="ngForm"> ...

Creating an enum from an associative array for restructuring conditions

Hey everyone, I have a situation where my current condition is working fine, but now I need to convert it into an enum. Unfortunately, the enum doesn't seem to work with my existing condition as mentioned by the team lead. Currently, my condition loo ...

Rxjs: handling arrays of observables by processing them in batches

I am facing a scenario where I have an array of observables. Let's say: const responses$: Observable<Response>[] = [ this.service.get(1), this.service.get(2), this.service.get(3), this.service.get(4) ]; My goal is to process ...

How to Measure the Length of an Undefined Value in Jasmine Angular Unit Tests

Here's a function that I have: updateParts(enviromentContainsAllParts: PartsContainsAllParts): Observable<boolean> { const enviroment = cloneDeep(this.enviroment); enviroment.containsAllPart = enviromentContainsAllParts.containsAllPart ...

Encountering a problem in a MEAN application while sending a PUT request

I am encountering an issue while developing a todos app in MEAN stack with MongoDB installed locally. The error I am facing is related to the PUT request. Any assistance on resolving this problem would be greatly appreciated. Error: Argument passed in mus ...

Creating descriptions for types in Vue.js using TypeScript

When running this code snippet, you might encounter the error message 'description' does not exist in PropValidator export default Vue.extend( { name: 'something', props: { 'backgro ...

Has there been any official confirmation that the ~ (tilde) symbol resolves to node_modules/ in Angular when importing scss files?

When incorporating a tilde (~) in scss import paths within an Angular project, it seems to automatically expand to node_modules/. I have searched for official documentation confirming whether this behavior is intentional within Angular or webpack, but have ...

syncfusion export pdf demonstrating the toggle button's current state

Currently, I am using syncfusion for converting my page to PDF format. I have a toggle button that is default set to true. However, regardless of the actual state of the toggle button, it always appears as on (true) when exported to PDF. I attempted to s ...

Error in TypeScript: Objects can only specify properties that are known, and 'state' is not found in type 'Partial<Path>'

As I strive to pass props through a React Router Link, my goal is to include all the user props. The code below is causing an error, particularly where state: {...employee} is highlighted. Although I am relatively new to TypeScript, I am actively working ...

Using Typescript and React to assign an array object to state

Here is the situation: const [state, setState] = useState({ menuCatalog: true, menuCommon: true, fetched_data: [] }); An example of data I am trying to set to the state property "fetched_data" looks like this: [{"id": 1, "name": "some_name", " ...

Compose a message directed to a particular channel using TypeScript

Is there a way to send a greeting message to a "welcome" text channel whenever a new user joins the server (guild)? The issue I'm running into is that, when I locate the desired channel, it comes back as a GuildChannel. Since GuildChannel does not hav ...

Delay the execution until all promises inside the for loop are resolved in Angular 7 using Typescript

I am currently working on a project using Angular 7. I have a function that contains a promise which saves the result in an array as shown below: appendImage(item){ this.imageCompress.compressFile(item, 50, 50).then( result => { this.compressedI ...

The Console.Log function will not run if it is placed within the RXJS Tap operator

In my current setup, I have the following observables: this.authenticationService.isSignedIn() -> Observable<Boolean> this.user$ -> Observable<UserModel> I am in need of checking a condition based on both these observables, so I attempt ...

What is the best method in Angular to reset form errors after submission?

Manually setting error validation in my form using setErrors is not providing clear errors upon submission. I do not want to reset all control errors to null, as this may cancel out other existing errors on the control. After setting the error once, I ne ...

Leverage Formidable to directly stream content to Azure Blob Storage without the need to save it in the /tmp directory

An interesting example provided by Formidable can be found here: https://github.com/node-formidable/formidable/blob/master/examples/store-files-on-s3.js. It showcases how to upload a stream to an S3 bucket without saving the content to a temporary file, wh ...

Embedding a module within a fluid element

Creating a dynamic component and projecting template inside it can be done easily with the following code snippet - @Component({ selector: 'dynamic', template: ` <p>Dynamic Component</p> <ng-content></ ...

What is preventing me from using the "@" symbol to substitute the lengthy relative path in my __tests__ directory?

https://i.sstatic.net/U1uW3.png When I initialized my Vue project using vue-cli, I encountered an issue where the use of @ in my src folder was functioning correctly, but not in my __tests__ folder. I have already added configurations to my tsconfig.json ...

Exploring Angular2 Components: Utilizing ViewChild and ContentChild based on class

I am working on creating a flexible component structure that can expand and collapse based on certain conditions: <collapsable [expanded]="myFlag"> <div title>Summary</div> <div alternate-title>Heading</div> <div con ...