Comments (12)
It could be nice to document this!
from ngx-sweetalert2.
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.
Hi,
Importing swal directly didn't cross my mind :). That will do the trick. Thanks!
from ngx-sweetalert2.
⚠️ 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.
@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.
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.
I tried swal({ title: 'Keep things simple :)' });
but I gets error cannot find name
, I missing something?
from ngx-sweetalert2.
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.
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.
@cmn60 Did you find the answer? I am trying this but just cant figure out. : (
from ngx-sweetalert2.
@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.
@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)
- This library is not working under IIS HOT 1
- Stricts templates & Angular Language Service HOT 1
- open multiple alerts HOT 1
- Default value for *swalPortal directive input HOT 7
- error NG8001: 'swal' is not a known element: HOT 3
- Modal when appear delete overflow-y : auto to hidden severals times HOT 1
- CommonJS or AMD dependencies can cause optimization bailouts. HOT 3
- Module not found: Error: Package path ./public-api is not exported from package HOT 1
- Generic type 'ɵɵ*Declaration' requires between x and y type arguments. HOT 8
- Add support for color property on SwalComponent
- add support for angular 15
- Cannot find module @sweetalert2/ngx-sweetalert2 or its corresponding type declarations HOT 1
- for Angular 16 HOT 4
- Compilation errors after last update - Angular 15 HOT 6
- Sweet alert over dialog primeng
- Will the library be upgraded to v17 of Angular? HOT 1
- Support for Angular 17 HOT 7
- Angular 17 - Stanalone components HOT 1
- Content shrinking when popup opens HOT 2
- Error during installation in Angular 17 HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngx-sweetalert2.