question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issues calling service after submission w/ Angular 2

See original GitHub issue

Hi there,

I’m using Angular 2 w/ SweetAlert2.

swal({
      title: 'Message All Models',
      input: 'textarea',
      inputPlaceholder: 'Type your message here',
      showCancelButton: true
    }).then(function() {
      this.modelService.messageModels()
    }, function(dismiss) {
      console.log('Dismissed')
 });

PROBLEM: I’m able to run this service outside of the swal() but for some reason when I run after the then() I get the following error:

ERROR:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property ‘modelService’ of undefined TypeError: Cannot read property ‘modelService’ of undefined at model-list.component.ts:174 - [ this is where service is called ]

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
toveruxcommented, Sep 21, 2017

This is not a bug, this is a misuse of the language 😄

@m1xf1t: replace :

.then(function() {
  this.modelService.messageModels()
})

with :

.then(() => {
  this.modelService.messageModels()
})

// shorter :
.then(() => this.modelService.messageModels())

// equivalent to :
.then((function() {
  this.modelService.messageModels()
}).bind(this))

That should work. The arrow function expression will bind the current context of this on the anonymous function being created (here, the component). Otherwise, using function, the scope of this is volatile (not captured by the closure, contrarily to the other variables) and determined at the call site (inside SweetAlert).

I suggest you to read one of the articles here: https://www.google.com/search?q=this+context+javascript (sorry for the Google link, no offense, I just don’t have a particular article in mind).

1reaction
limontecommented, Sep 21, 2017

For bug reports, please include:

  • a jsfiddle in order for us to be able to reproduce the issue.
  • expected behavior of the plugin
  • actual behavior

JSFiddle template: https://jsfiddle.net/limon/ad3quksn/

See Contributing.md

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2 How do I call a service method when an observable ...
After get result from server, put it to array. Angular will update the data in the template itself, this is the fastest way....
Read more >
Top 18 Most Common AngularJS Developer Mistakes - Toptal
Common Mistake #16: Not Running The Unit Tests In TDD Mode. Tests will not make your code free of AngularJS error messages. What...
Read more >
Angular 4 form submit is not working #9019 - GitHub
I am using Angular 4 for my application development. When I submit my form it's calling the browser URL for posting.
Read more >
Getting Past Hello World in Angular 2 - SitePoint
This post will give you a quick introduction to these concepts and how they apply to your Angular 2 applications.
Read more >
Communicating with backend services using HTTP - Angular
Most front-end applications need to communicate with a server over the HTTP protocol, to download or upload data and access other back-end services....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found