Advertisement
Guest User

Untitled

a guest
Oct 18th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. export class LoginComponent implements OnInit {
  2.  
  3. constructor(private fb: FormBuilder, private loginService: LoginService) { }
  4.  
  5.  
  6. submitError = false; // set "true" to show error
  7.  
  8. onSubmit(){
  9.  
  10. this.submitError = false;
  11. this.submitErrorText = "";
  12. this.model = this.loginForm.value;
  13. this.loginService.authenticate(this.model.login_id, this.model.password)
  14. .then(res => {
  15.  
  16. if(res.status == 404){
  17.  
  18. this.submitError = true;
  19.  
  20. }
  21. else{
  22. // do something
  23. }
  24.  
  25. }).catch(function(err: any){
  26.  
  27. this.submitError = true; // Unhandled promise rejection TypeError: Cannot set property 'submitError' of undefined
  28.  
  29.  
  30.  
  31. });
  32.  
  33.  
  34.  
  35.  
  36. }
  37.  
  38. import {Injectable} from '@angular/core';
  39. import { Http } from '@angular/http';
  40.  
  41. import 'rxjs/add/operator/toPromise';
  42.  
  43. @Injectable()
  44. export class LoginService{
  45.  
  46.  
  47. private auth_url = "/login/api/auth";
  48.  
  49. constructor(private http: Http){};
  50.  
  51. authenticate(username: String, pass: String): Promise<any>{
  52. return this.http.post(this.auth_url, {username: username,pass: pass})
  53. .toPromise()
  54. .then(response => response.json())
  55. .catch(this.handleError);
  56. }
  57.  
  58. private handleError(error: any): Promise<any>{
  59.  
  60. return Promise.reject(error.message || error);
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement