The compatibility issues between Angular 5 and materialize-css (v 1.0.0) are causing obstacles in functionality

I attempted to implement the solution found on this post:
Unfortunately, the solution didn't work as expected. I am working with Angular and Typescript in my project.

Here is a snippet of my Typescript class:

import { Component, OnInit, AfterViewInit, ViewChild, ElementRef } from '@angular/core';
import * as M from 'materialize-css/dist/js/materialize';

@Component({
    selector: 'app-list-nav',
    templateUrl: './list-nav.component.html',
    styleUrls: ['./list-nav.component.css']
})
export class ListNavComponent implements OnInit, AfterViewInit {

    @ViewChild('collapsible') elCollapsible: ElementRef;

    constructor() { }

    ngOnInit() {

    }

    ngAfterViewInit() {
        const instanceCollapsible = new M.Collapsible(this.elCollapsible.nativeElement, {});
    }

}

In the HTML section, the element has an ID of 'collabsible', and the Class 'collabsible'. Despite this setup, I encountered the following error message:

 ng:///AppModule/ContentComponent.ngfactory.js:12 ERROR TypeError: Cannot read property 'nativeElement' of undefined
    at ListNavComponent.ngAfterViewInit (webpack-internal:///../../../../../src/app/list-nav/list-nav.component.ts:20)
    at callProviderLifecycles (webpack-internal:///../../../core/esm5/core.js:12922)
    at callElementProvidersLifecycles (webpack-internal:///../../../core/esm5/core.js:12889)
    at callLifecycleHooksChildrenFirst (webpack-internal:///../../../core/esm5/core.js:12872)
    at checkAndUpdateView (webpack-internal:///../../../core/esm5/core.js:14027)
    at callViewAction (webpack-internal:///../../../core/esm5/core.js:14369)
    at execComponentViewsAction (webpack-internal:///../../../core/esm5/core.js:14301)
    at checkAndUpdateView (webpack-internal:///../../../core/esm5/core.js:14024)
    at callViewAction (webpack-internal:///../../../core/esm5/core.js:14369)
    at execEmbeddedViewsAction (webpack-internal:///../../../core/esm5/core.js:14327)

Answer №1

When using @ViewChild, you are unable to query by the css id directly. Instead, you must include a template variable.

For example, instead of

<div id="collapsible"></div>

You should use

<div #collapsible></div>

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

What causes the failure of $event binding when using RowGroup tables with PrimeNG?

I have successfully implemented RowGroup to store 3 different tables, which is working great. However, I am facing an issue with the onRowSelect function not functioning properly. When I click on any row of the RowGroup tables, nothing happens. On another ...

The async pipeline fails to update with fresh data from the array

I have a challenge with my app that requires making approximately 500 API requests and displaying the data as it is being fetched. Currently, I am using an async pipe with an observable to achieve this. While attempting to update the view upon completion ...

Struggling to properly interpret and process the latest data being sent from an Angular single page application (SPA

Currently, I am developing a Single Page Application that utilizes Angular 8 on the frontend and Laravel on the backend. The application involves performing CRUD operations, and specifically when dealing with editing functionality, I encounter an issue. Th ...

Storing browser cookies in Azure Static Web App

I recently set up a solution on Azure with a web API in .NET 6 deployed to App Service, and a front-end application in Angular deployed to Azure Static Web App. I made sure to configure CORS on the back-end App Service to allow the front-end origin. Howeve ...

Using TypeScript to assign string array values to object properties

I am working with a string array: values: string['myName', 'myLastname', 'myAge'] and my goal is to assign each value to a property of an object like this: myModel={name:'', lastname:'', age:''} o ...

Property does not exist on object error is thrown by Angular httpClient.getResult

constructor(private http: HttpClient) { } ngOnInit() { this.http.get('url').subscribe(data => { console.log(data); console.log(data.login); }); } } When looking at the data in the console, I noticed that the property &ap ...

What is the best way to set up the parser and plugins using ESLint's updated flat configuration?

How can ESLint be configured using the new "flat config" system (specifically with the eslint.config.js file) to work seamlessly with both @typescript-eslint/eslint-plugin and /parser? I have been struggling to make ESLint's new configuration system ...

The CORS policy specified in next.config.js does not appear to be taking effect for the API request

I am currently working on a Next.js application with the following structure: . ├── next.config.js └── src / └── app/ ├── page.tsx └── getYoutubeTranscript/ └── getYoutubeTranscript.tsx T ...

"Exploring the possibilities of Angular 6 through custom pipe

Is there a way to integrate a custom pipe from an Angular 6 library into my main app? I have been attempting to do so in the following manner: @NgModule({ declarations: [ SomePipe ], exports: [ SomePipe ]}) Within public_api.ts: export * fr ...

Typescript interface design for nested objects in a hierarchical structure

When data is received from the server in JSON format, it typically looks like the example below (details have been modified): { "apple": { "fruitName": "apple", "types": { "greenApple": { ...

Issue with Progressive Web App functionality when using Angular Pre Rendering

Currently, I am working on an Angular 10 Universal project. Whenever I execute the following command: "build:ssr": "ng build --prod && ng run PROJECT:server:production && node dist/PROJECT/server/main.js", I can see th ...

Strategies for increasing the number of images in Angular

At the start, 15 images are displayed from the API. However, the "Get dogs" button should load an additional 15 images each time it's clicked, but currently, it doesn't work. How can we fix this issue? http.service.ts - a service that interacts ...

Utilize ngx-translate in Ionic 2 for translating menu items

I have successfully implemented ngx-translate for multi-language support in my application. However, I am now looking to extend this functionality to my menu items. How can I achieve this for my 3 menu items with different titles? ts file appPages: Pag ...

Warning issued by Angular 7 indicating the usage of the routerLink directive and advising against navigation triggered outside the Angular zone

Encountering difficulties while working with the Angular framework to ensure smooth functioning of my application, I am currently facing an obstacle with routing. The structure includes a main AppComponent and a app-routing.module.ts file for navigation ma ...

Using cdk-virtual-scroll-viewport in combination with Angular and flex-layout (row and wrap)

I am interested in implementing virtual scrolling with flexlayout. For example, I would like the following code to be compatible with virtual scrolling: <div fxLayout="row wrap" fxLayoutAlign="start center"> <div *ngFor=&qu ...

Assign a class to the following element using an Angular 2 Directive

I have a dropdown menu and I want to incorporate an Angular2 directive to control the opening and closing of this dropdown. How can I apply the open class to the latest-notification div, knowing that my directive is applied to the button tag? Below is my ...

Avoid passing the ngrxStore object between separate custom elements

I am working with an Angular custom element that is exported as a web component. This custom element dispatches actions to a search state in order to perform a search functionality. The handling of these search state actions is carried out within an angula ...

Utilize the class or interface method's data type

In the context of a child component calling a callback provided by its parent, this situation is commonly seen in AngularJS. As I am utilizing TypeScript, I aim to implement strong typing for the callback in the child component. Here is the initial stat ...

Swapping out a class or method throughout an entire TypeScript project

Currently, I am working on a software project built with TypeScript. This project relies on several third-party libraries that are imported through the package.json file. One such library includes a utility class, utilized by other classes within the same ...

Creating a type-safe method wrapper in TypeScript based on function names

Many Q&As discuss creating a function wrapper in TypeScript, but my question is how to do the same with named methods. I am interested in writing something similar to this JavaScript code: function wrap(API, fnName, fn) { const origFn = API.p ...