Shut down an ionic modal using a service

Currently, I am using Ionic 3 (Angular 4) and I am interested in closing an Ionic modal within a service. Is this feasible?

View the DataService.ts screen

I am aiming to execute a close() function within another function of my DataService.ts to effectively close my deleteconfirm.ts modal.

Answer №1

After reviewing your code, it appears that your service will need information about the view in order to function properly.

One solution is to include an instance of the modal within your service whenever the modal is opened. This way, you can easily reference that instance and close it using the service.

Here is a hypothetical example to illustrate the concept:

// Sample component
export class MyComponent implements OnInit {
    modal: Modal;

    constructor(private modalService: ModalService) {}

    ngOnInit() {
        this.modalService.modal = this.modal;
    }

    close() {
        this.modalService.close();
    }
}

// Sample service
export class ModalService {
    _modal: Modal;

    set modal(modal: Modal) {
        this._modal = modal;
    }

    close() {
        if (this._modal) {
            this._modal.close();
            this._modal = null;
        }
    }
}

By utilizing this method, you create flexibility as any component can close the modal simply by injecting the ModalService.

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

Difficulty establishing a connection between data in app.js and index.html using Ionic framework

Struggling with connecting index.html to get data for my Ionic mobile app. Any tips or guidance would be highly appreciated as I'm new to this! The goal is to display a list of restaurants in the app, utilizing Cards Images to showcase an image, logo ...

What sets apart the ? operator from incorporating undefined in the type declaration?

Can you explain the distinctions among these options? type A = {a?: string} type A = {a: string | undefined} type A = {a?: string | undefined} In what scenarios would one be preferred over the others? For more information, visit: https://github.com/mic ...

Dealing with the possibility of an empty array when accessing elements by index in Typescript

What is the best way to handle accessing elements by index in an array in Typescript when the array can be empty, resulting in potentially undefined elements? I am developing a simple game using React and Typescript where I have a variable named game whic ...

How come the props aren't being passed from the parent to the child component? (React / TypeScript)

Learning TypeScript for the first time and facing an issue with passing props from parent to child components. The error seems to be related to the type of props, but I'm not sure how to fix it since all types seem correct. Error message: "Property ...

Clicking on an Angular mat-checkbox requires two clicks to toggle between checked and unchecked states

My checkboxes are populating dynamically, but I am facing an issue when trying to unselect them. It takes two clicks to uncheck the checkbox. enter code here <mat-tree [dataSource]="dataSource" [treeControl]="treeControl"> ...

Experimenting with an Angular 2 component using a simulated service

Currently, I am experimenting with testing an Angular2 component that relies on a service. In order to conduct the test effectively, I aim to provide a stubbed service. However, it appears that the test component does not recognize this stubbed service. ...

Using ES6 proxy to intercept ES6 getter functions

I have implemented a proxy to track all property access on instances of a class, demonstrated in the code snippet below: class Person { public ageNow: number; public constructor(ageNow: number) { this.ageNow = ageNow; const proxy = new Proxy( ...

Error in Typescript cloud function: response object is not callable

My cloud function is structured like this: // const {onSchedule} = require("firebase-functions/v2/scheduler"); // The Cloud Functions for Firebase SDK to set up triggers and logging. import * as logger from "firebase-functions/logger&quo ...

The Angular 6 test command using npm has encountered a failure

I've been encountering a disconnect error while attempting to run Angular test cases. With over 1500 test cases written, it appears that the sheer volume may be causing this issue. Is there a way to resolve the disconnect error when running such a lar ...

When implementing .forEach, an object within the array is mistakenly duplicated

Struggling to populate an array of objects with unique data using the .forEach function to display feedback in the Administrator page of my portfolio. However, encountering an issue where the objects from 'd' are getting duplicated in the 'f ...

"Adjusting the position of an Ionic Menu on-the-fly

As I strive to update the Ionic 3 Menu side dynamically when the user changes the language, a challenge arises for RTL languages where the menu needs to be on the right instead of the default left. To tackle this issue, I have subscribed to the TranslateS ...

Upon updating AngularFire, an error is thrown stating: "FirebaseError: Expected type 'Ea', but instead received a custom Ta object."

I have recently upgraded to AngularFire 7.4.1 and Angular 14.2.4, along with RxFire 6.0.3. After updating Angular from version 12 to 15, I encountered the following error with AngularFire: ERROR FirebaseError: Expected type 'Ea', but it was: a c ...

Error in Angular 7: params.map function is undefined

When attempting to launch my Angular 7 application with ng serve, I suddenly encountered the following error: ERROR in params.map is not a function I am unsure of the origin of this error as ng isn't providing much information. I have attempted to ...

Attempting to change the id property into a Mongoose ObjectId

Here is a snippet of my code: acceptContactRequest: async ( _: any, { userId }: IUserInfo, context: ExpressContext ) => {.....} The interface IUserInfo looks like this: export interface IUserInfo { username: string; password: ...

Incorporating drawable resources with Cordova

I'm looking to include a new drawable resource in my Cordova project. I've had success adding the icon and splashscreen items, which were copied to platform/android/res/drawable without any issues. However, I'm running into trouble when atte ...

Leveraging the ng-template in a broader context

I need to create a recursive menu from JSON data with a specific structure. The JSON looks like this: this.menus = [{ "id": 1, "title": "Azure", "class": "fa-cloud", "url": "#", "menu": [{ "id": 121, ...

Struggling to combine interface with import and local variables - any solutions?

Although examples have demonstrated the merging of interfaces in a single file, I am facing challenges when trying to merge interfaces that are located in different files. I want to clarify that I am not extending any modules, just interfaces. /types/ind ...

Issue when rendering <options> while looping through country object in the country list

To retrieve a list of all countries in my React project written in TypeScript, I am utilizing the countries-list library which can be found here. My intention is to create a <Form> that includes a <Select> dropdown menu populated with the coun ...

The Sourcemap is not correctly aligning with the expected line number

Currently working with the angular2-webpack-starter technology and utilizing VSCode along with Chrome debugger. After numerous attempts, I was able to successfully set a breakpoint, but it appears that the line mapping is incorrect. The issue persists in ...

What is the reason behind being able to assign unidentified properties to a literal object in TypeScript?

type ExpectedType = Array<{ name: number, gender?: string }> function go1(p: ExpectedType) { } function f() { const a = [{name: 1, age: 2}] go1(a) // no error shown go1([{name: 1, age: 2}]) // error displayed ...