最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sweetalert2 - change displayed text dynamically - Angular2 - Stack Overflow

programmeradmin2浏览0评论

How could I change the text after I get the response asynchronously?

  var swText = "<h3>Importiranje kvota, molimo sačekajte...</h3>";
  swal({ html: swText });
  swal.showLoading();
  this.koloService.importExcelFileWithOdds(this.brojKola, fileList)
      .subscribe(
          data => { swText = "<h3>Importiranje završeno!</h3>"; swal.hideLoading(); },
          error => {  swText = "<h3>Importiranje završeno!</h3>"; swal.hideLoading(); });

Also, how could I hide the progress alert after I get the response from server? The progress alert looks like this while the data is being imported:

And after I get the response from the server, progress alert doesn't hide, there is just an OK button which will close the alert - I want it to close as soon as I get the response from the server.

How could I change the text after I get the response asynchronously?

  var swText = "<h3>Importiranje kvota, molimo sačekajte...</h3>";
  swal({ html: swText });
  swal.showLoading();
  this.koloService.importExcelFileWithOdds(this.brojKola, fileList)
      .subscribe(
          data => { swText = "<h3>Importiranje završeno!</h3>"; swal.hideLoading(); },
          error => {  swText = "<h3>Importiranje završeno!</h3>"; swal.hideLoading(); });

Also, how could I hide the progress alert after I get the response from server? The progress alert looks like this while the data is being imported:

And after I get the response from the server, progress alert doesn't hide, there is just an OK button which will close the alert - I want it to close as soon as I get the response from the server.

Share Improve this question asked Feb 5, 2018 at 8:29 Željko KrnjićŽeljko Krnjić 2,3662 gold badges18 silver badges25 bronze badges 3
  • 2 "destroying the initial swal and creating a new one in your data and error response handling part of your response" is an option for your case? – Noxthron Commented Feb 5, 2018 at 8:50
  • Could use that I guess in this use case, but is there a functionality to change the text without destroying the swal? – Željko Krnjić Commented Feb 5, 2018 at 9:07
  • @ŽeljkoKrnjić any luck, i am having similar problem. – Deep 3015 Commented Mar 10, 2019 at 13:25
Add a ment  | 

2 Answers 2

Reset to default 5

Update (2022): use Swal.update() method:

Swal.fire('After mignight I will turn into a pumpkin')
swal.showLoading()

// change the title after 2 seconds
setTimeout(() => {
  Swal.update({ title: 'I am a pumpkin now!' })
  Swal.hideLoading()
}, 2000)
<script src="https://cdn.jsdelivr/npm/sweetalert2@11"></script>


There are all kinds of getters for accessing different parts of SweetAlert2: https://sweetalert2.github.io/#methods

You are looking for Swal.getTitle():

Swal.fire('After mignight I will turn into a pumpkin')
swal.showLoading()

// change the title after 2 seconds
setTimeout(() => {
  Swal.getTitle().textContent = 'I am a pumpkin now!'
  Swal.hideLoading()
}, 2000)
<script src="https://cdn.jsdelivr/npm/sweetalert2@11"></script>

My answer has nothing about Angular2 but a simple solution to keep in mind, in Swal options you can do this

confirmButtonText: (() => {
   if (your_data) {
     return "something"
   } else return "something else"
})()
发布评论

评论列表(0)

  1. 暂无评论