Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
La variable de clase angular pierde valor

Usando Angular2, tengo un http.get de la siguiente manera:

 this.http.post('http://localhost/Authenticated/Token', loginData, options).subscribe(function (data) { console.log(JSON.stringify(data, null, 4)); if (data.status === 200) { this.response = data.json(); this.access_token = this.response.access_token; alert(this.access_token); } });

El valor de this.access_token se establece correctamente en tiempo de ejecución. Pero pierde valor una vez terminado el método. Estoy tratando de usarlo en otro lugar en un método pero dice indefinido.

EDITAR:

Este es el método que se adjunta a un disparador en un botón, al que llamo después de ver la respuesta del método anterior:

 getBlogEntries() { alert(this.access_token); let headers = new Headers(); headers.append('Authorization', 'Bearer ' + this.access_token); let options = new RequestOptions({ headers: headers }); if (this.access_token) { this.http.get('http://localhost/Authenticated/BlogEntries/', options).subscribe(result => { this.blogEntries = result.json().Data; console.log(this.blogEntries); }); } }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Pruebe el siguiente código:

 this.http.post('http://localhost/Authenticated/Token', loginData, options).subscribe(function (data) { console.log(JSON.stringify(data, null, 4)); if (data.status === 200) { this.response = data.json(); this.access_token = this.response.access_token; alert(this.access_token); } }.bind(this));

Debería resolver su problema.

Una alternativa más si codifica en ECMA 6.0:

 this.http.post('http://localhost/Authenticated/Token', loginData, options).subscribe((data) => { console.log(JSON.stringify(data, null, 4)); if (data.status === 200) { this.response = data.json(); this.access_token = this.response.access_token; alert(this.access_token); } });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!