Coder Social home page Coder Social logo

Comments (12)

gcaraciolo avatar gcaraciolo commented on June 4, 2024 3

It could be nice to document this!

from ngx-sweetalert2.

toverux avatar toverux commented on June 4, 2024 2

Use the loader service:

import { SweetAlert2LoaderService } from '@sweetalert2/ngx-sweetalert2';

@Injectable()
export class MyService {
    public constructor(private readonly sweetAlert2Loader: SweetAlert2LoaderService) {
    }
    
    public async doSomething() {
        const swal = await this.sweetAlert2Loader.swal;

        swal.xxx(); // <--- this sweetalert2 instance comes from the provideSwal function you defined in your module
    }
}

from ngx-sweetalert2.

netopolit avatar netopolit commented on June 4, 2024 1

Hi,
Importing swal directly didn't cross my mind :). That will do the trick. Thanks!

from ngx-sweetalert2.

toverux avatar toverux commented on June 4, 2024 1

⚠️ Edit: New Answer

Don't do this anymore when using ngx-sweetalert2. Retrieve a reference to the swal API using the SweetAlert2LoaderService so you still benefit from the lazy loading of the sweetalert2 library (loads it when you need it), and your module configuration in forRoot()/forChild() if you have any 😉


Hi @Slzr, when using TypeScript, you should use:

import swal from 'sweetalert2';

Depending on you tsconfig.json strictness, this may throw a warning saying that sweetalert2 has no default export. In that case, use:

import * as swal from 'sweetalert2';

It's true that SweetAlert2 also defines itself on window, but it's better to use it like a module (and that's how it's described in the definition file).

from ngx-sweetalert2.

toverux avatar toverux commented on June 4, 2024 1

@kctann It won't be easy as an Angular service can't hold a template. You would need to declare your service, and then have a component that is always loaded by your app (e.g. referenced by your root component), and that component could depend on the service, and register the SwalComponent instances of its template (retrieved in TypeScript via @ViewChild) to the service.

So yeah it's doable but it seems vastly over-engineered for no benefit in your case. I'd recommend you to stick with sweetalert2 alone for that.

If you still intend to use this integration in parallel for other modals, that are specific to some components for example, be careful to NOT import sweetalert2 in your service with import Swal from 'sweetalert2', instead, import it like this so you still benefit from lazy-loading!

And if you don't need ngx-sweetalert2 anymore, uninstall it, follow the SweetAlert2 docs instead and enjoy as well!

from ngx-sweetalert2.

toverux avatar toverux commented on June 4, 2024

Hello,

Although it was originally planned, I don't really see the interest of this, since you can just:

swal({ title: 'Keep things simple :)' });

I'm not saying that a service is bad, but the amount of complexity it adds (to the library and userland code) does not seem so worthy (moreover, the goal of this library is also to enforce a declarative approach over an imperative one, and therefore reduce the amount of code in the code-behind).

However, I'm still open to discussion, and if you can provide a real use case for that I'd be happy to implement such a thing!

from ngx-sweetalert2.

salazarr-js avatar salazarr-js commented on June 4, 2024

I tried swal({ title: 'Keep things simple :)' }); but I gets error cannot find name, I missing something?

from ngx-sweetalert2.

cmn60 avatar cmn60 commented on June 4, 2024

Hello,

Although it was originally planned, I don't really see the interest of this, since you can just:

swal({ title: 'Keep things simple :)' });

I'm not saying that a service is bad, but the amount of complexity it adds (to the library and userland code) does not seem so worthy (moreover, the goal of this library is also to enforce a declarative approach over an imperative one, and therefore reduce the amount of code in the code-behind).

However, I'm still open to discussion, and if you can provide a real use case for that I'd be happy to implement such a thing!

Hi;
I'm trying to use an angular component as a swal, and create a service to open/close it.
the idea is to have a common swal that can be used by multiple components.
is that possible ?

from ngx-sweetalert2.

lixaotec avatar lixaotec commented on June 4, 2024

Dear @toverux and @limonte

How to call it from a service that has been definied ForRoot with heightAuto ?
If i do import swal from 'sweetalert2'; it seems not to respect the heightAuto defined in root

thanks

from ngx-sweetalert2.

kctann avatar kctann commented on June 4, 2024

@cmn60 Did you find the answer? I am trying this but just cant figure out. : (

from ngx-sweetalert2.

toverux avatar toverux commented on June 4, 2024

@kctann it is possible with the library, but then, given your use case, why aren't you just using the sweetalert2 library alone? Or is there some feature of this integration you need?

from ngx-sweetalert2.

kctann avatar kctann commented on June 4, 2024

@toverux ,thank you for the quick reply. I am trying to make my alert consistent (after calling API response), so I would like to pop out it with angular's service. I was wondering if we can have a bunch of different style's alert in one component, which is good to manage and encapsulate not only html but also css style. And when we get response , I can switch to the one to pop out. Or maybe the best way to do this is with the js library for now?
Thank you so much.

from ngx-sweetalert2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.