The functionality of MaterializeCSS modals seems to be experiencing issues within an Angular2 (webpack) application

My goal is to display modals with a modal-trigger without it automatically popping up during application initialization. However, every time I start my application, the modal pops up instantly. Below is the code snippet from my component .ts file:

import { Component, OnInit, OnChanges, EventEmitter } from '@angular/core';
import { Location } from '@angular/common';
import { CompetenceService } from './../../core/services/competence-data.service';
import { Competence } from './../../models/competence';
import { Observable } from 'rxjs/Observable';
import { MaterializeAction } from 'angular2-materialize';

@component({
selector: 'competence',
templateUrl: './template/competence.html'
})

export class competenceComponent implements OnInit {

    competence: Competence;
    competenceliste: Observable<Array<Competence>>;

    constructor(private service: CompetenceService) { }

    ngOnInit() {
        this.competenceliste = this.service.GetFromCV();    
    }

    modalActions = new EventEmitter<string | MaterializeAction>();

    openModal() {
        this.modalActions.emit({ action: "modal", params: ['open'] });
    }

    closeModal() {
        this.modalActions.emit({ action: "modal", params: ['close'] });
    }

    public modifcompetence() {
        this.service.Update(this.competence).subscribe();
        this.competence = null;
        this.competenceliste = this.service.GetFromCV();
    }

    delete(competence: Competence) {
        if (confirm("Voulez-vous vraiment supprimer cette compétence ?")) {
            this.service.Delete(competence).subscribe();
            this.competence = null;
            this.competenceliste = this.service.GetFromCV();
        }
    }

    updateButton(competence: Competence) {
        this.competence = competence;   
    }
}

And here's my corresponding HTML snippet:

<button class="btn-floating btn-small waves-effect waves-light grey btnmodif modal-trigger" (click)="openModal()">
      <i class="material-icons">mode_edit</i>
</button>

<div *ngIf="competence">
    <div id="modal1" class="modal" materialize="modal" [materializeParams]="[{dismissible: false}]" [materializeActions]="modalActions">
        <div class="modal-content">
            <h4>Modifier une compétence</h4>
            <div class="input-field col s6">
                <input type="text" class="form-control" id="modifcompetence"
                       required
                       [(ngModel)]="competence.name" name="modifcompetence">
                <label class="active">Nom</label>
            </div>
            <div class="input-field col s6">
                <input type="text" class="form-control" id="modifcompetence"
                       required
                       [(ngModel)]="competence.categorie" name="modifcompetence">
                <label class="active">Catégorie</label>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="waves-effect waves-light btn grey">Annuler</button>
            <button type="button" (click)="modifcompetence()" class="waves-effect waves-light 
                   btn">Modifier</button>
        </div>
   </div>

The technologies I am using are:

  • webpack
  • jquery 2.2.4
  • materializecss 0.98.2
  • angular-materialize 15.0.8
  • hummerjs 2.0.4

Answer №1

Encountering various challenges with integrating Materialize and Angular has been a common issue for me. I am not sure what issues you are facing in your code, but one possible solution is to consider using the materialize framework designed specifically for Angular found at . This version of Materialize has proven to be more compatible with Angular. A properly functioning modal should automatically handle opening and closing triggers without requiring additional implementation.

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

Guide on updating a value in a Firestore document using Firebase

I am working on updating a specific portion of a document in my Firebase collections structure, specifically the phonebook(map) section. https://i.sstatic.net/UmHot.png When attempting to modify the document, I encountered an error saying Invalid documen ...

Should FormBuilder be utilized in the constructor or is it considered a poor practice?

section, you can find an example of implementation where declarations for formBuilder and services are done within the constructor(). While it is commonly known that using services inside the constructor() is not a recommended practice and should be done ...

Error in Typescript: Array containing numbers is missing index property `0`

This is the code for my class: class Point{ coordinates: [number, number, number]; constructor(coordinates: [string, string, string]) { this.coordinates = coordinates.map((coordinate) => { return Math.round(parseFloat(coordinate) *100)/ ...

Angular hover effect only triggered on specific item in the list

<div class="col-sm-2" style="margin-top: 15px;" *ngFor="let song of librarySongs"> <div id="container" (mouseover)="setOptions(true)" (mouseout)="setOptions(false)"> ...

The data type 'T' cannot be assigned to type 'T'

Having extensive experience as a javascript developer, I recently delved into learning C# as my first statically typed language. My upcoming project involves using TypeScript, so I've been refreshing my knowledge on it. Below is the code I have writt ...

Adding local images to Excel can be easily accomplished using Office Scripts

Hello, I've been attempting to replace Excel cells that contain image filepaths with the actual images themselves. I found an example in Office Scripts that shows how to insert images with online URLs but doesn't mention anything about inserting ...

Why isn't the Angular2 ngIf directive updating the DOM?

I am encountering issues with finding the right expression for ngIf to evaluate properly. After following a basic Angularfire2 example, I have successfully managed to log in and out. import { Component } from '@angular/core'; import { AngularFi ...

Comparison: executing an immediately invoked function expression (IIFE) and a separate

During my recent refactoring of some legacy code, I stumbled upon the following example: // within another function const isTriggerValid = await (async () => { try { const triggers = await db.any(getTriggerBeforeBook, { param ...

Discover a Simple Trick to Enhance tsc Output: Unveil the Art of

When I work on a TypeScript project, I typically use the watch mode of the compiler: tsc --watch However, one issue I face is that it's challenging to identify errors in the output since they are displayed as plain text: Sometimes I don't even ...

The useAutocomplete function in Material-UI fails to consider the disabled

Currently, I am working on developing my own Autocomplete component by utilizing the useAutocomplete hook from the mui/base package. Most parts of the implementation are functioning correctly, except for the disabled option. My expectation is that the com ...

Mapping a list of records by a nested attribute using Typescript generic types

In my current project, I am implementing a unique custom type called "RecordsByType", which is currently undefined in the code snippet below. Within this snippet, I have various types that represent database records, each with its type defined within a ne ...

Angular - Unable to access property '$invalid' because it is null

Working on my login page with angular and typescript. Clicking the submit button should trigger the login function in the controller, but if the form is invalid, it should just return. New to typescript, I keep running into an error when trying to add an ...

Styling Angular2 Material Dialog: the perfect fit

The Angular2 material team recently unveiled the MDDialog module at https://github.com/angular/material2/blob/master/src/lib/dialog/README.md I am interested in customizing the appearance of Angular2 material's dialog. Specifically, I want to adjust ...

"I have successfully removed the URL from index.html. However, I am now wondering how I can include them in app

I have modified the URL in index.html to remove query parameters, but now I need my app.component.ts file to still be able to access and work with those query params using ActivatedRoute. However, when I implemented the script in index.html to remove query ...

Learn about how to pass HTML content as an input variable in Angular 8

When it comes to passing input props or data, we typically use @Input. Another option is using <ng-content> to insert a bunch of HTML into the children component. Is there any way to pass HTML as an Input in Angular? For example, using @Input html1 a ...

What is the best way to transform the data stored in Observable<any> into a string using typescript?

Hey there, I'm just starting out with Angular and TypeScript. I want to get the value of an Observable as a string. How can this be achieved? The BmxComponent file export class BmxComponent { asyncString = this.httpService.getDataBmx(); curr ...

Detecting URL changes on new windows in Angular Typescript: How to do it?

I am currently working with the updated version of Angular 6. My task involves opening a new browser window based on user input, which includes a specific return URL. After the return URL is fully loaded, I need to access and extract a particular element f ...

The environment production variable is perpetually set to true within the app.module.ts file

I'm currently facing an issue with setting a configuration in my app.module file that should differ based on whether I'm in production or not. Within my environment.ts file, I have the following: export const environment = { production: false ...

Solution for dealing with error 'Cannot find Property length on type {} in React using TypeScript

Any ideas on how to fix the error "Property 'length' does not exist on type '{}'"? Below is the code snippet causing the issue: //component const SearchResults = ({ results }: { results: {} }) => { let pageCount = results? ...

When should services be included within providers?

After completing a small Angular 2 project, I compared my work with the provided code and realized that in the app.module.ts file, only one of the two services I created was included by the tutor. users.service.ts import { Component, Injectable } fro ...