Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

368
Visualizações
Angular2 no puede acceder a 'esto' dentro de una promesa

No puedo llamar a una función dentro de la promesa del complemento ng2-sweetalert2

 swal({ title: 'Are you sure?', text: "You won't be able to revert this!", type: 'warning', showCancelButton: true, confirmButtonText: 'Yes, delete it!' }).then(function(x) { this.removeNote(key); swal( 'Deleted!', 'Your file has been deleted.', 'success' ); }, function(e){ console.log('Cancelled'); }); removeNote(key){ this.todo.remove(key); this.afService.closeNote(key); }

this.removeNote() causa un error.

 EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'removeNote' of undefined

¿Cómo supero esto? Usé NgZone pero recibo el mismo error

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Suponiendo que está usando TypeScript, podría usar laexpresión de función de flecha , que conserva el valor de this .

 swal({...}).then((x) => console.log(this)); // now 'this' is your component
about 4 years ago · Santiago Trujillo Relatório

0

esto es porque this se refiere a la promesa misma. hacer esto :

 let self = this; swal({ title: 'Are you sure?', text: "You won't be able to revert this!", type: 'warning', showCancelButton: true, confirmButtonText: 'Yes, delete it!' }).then(function(x) { self.removeNote(key); swal( 'Deleted!', 'Your file has been deleted.', 'success' ); }, function(e){ console.log('Cancelled'); }); removeNote(key){ this.todo.remove(key); this.afService.closeNote(key); }
about 4 years ago · Santiago Trujillo Relatório

0

También puede usar la siguiente manera:

 swal({}) .then(() => { <your angular 2 service call here...>})

El siguiente es el ejemplo de trabajo:

 customDialog(id,value){ swal({ title: 'Are you sure?', text: "Message", type: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'Yes', cancelButtonText: 'No', confirmButtonClass: 'btn btn-success alertboxmargin', cancelButtonClass: 'btn btn-danger alertboxmargin', buttonsStyling: false }).then(() => { this.services.testfunction(table,value,id) .subscribe( result => { if(result) { if(value) { swal('Message!','Message.','success') } else { swal('Message!','Message.','success') } } else { swal('Error!','Try again later.','error') } }); }, function (dismiss) { // dismiss can be 'cancel', 'overlay', // 'close', and 'timer' if (dismiss === 'cancel') { swal('Cancelled','No action performed!','error') } })//then closing } // Dialog Closing
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda