Guest User

Untitled

a guest
May 12th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. export interface User {
  2. id: number;
  3. login: string;
  4. password: string;
  5. }
  6.  
  7. import { Injectable } from '@angular/core';
  8. import {HttpClient, HttpHeaders} from '@angular/common/http';
  9. import {Observable} from 'rxjs/Observable';
  10. import {User} from './interfaces/User';
  11.  
  12.  
  13. const httpOptions = {
  14. headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  15. };
  16.  
  17. @Injectable()
  18. export class UserService {
  19.  
  20. constructor(private http: HttpClient) { }
  21.  
  22. public addUser(newUser: User): Observable<{}> {
  23. return this.http.post('http://localhost:8080/users/newuser', newUser);
  24. }
  25.  
  26. public extractData(res: Response) {
  27. const body = res.json();
  28. return body || {};
  29. }
  30.  
  31. }
  32.  
  33. import { Component, OnInit } from '@angular/core';
  34. import {UserService} from '../user.service';
  35. import {User} from '../interfaces/User';
  36.  
  37. @Component({
  38. selector: 'app-register',
  39. templateUrl: './register.component.html',
  40. styleUrls: ['./register.component.scss']
  41. })
  42. export class RegisterComponent implements OnInit {
  43.  
  44. users: User[];
  45.  
  46. constructor(private userService: UserService) {
  47. }
  48.  
  49. ngOnInit() {
  50.  
  51. }
  52.  
  53. addUser(emailText: string, passwordText: string) {
  54.  
  55. const newUser: User = ({
  56. id: null,
  57. login: emailText,
  58. password: passwordText
  59. });
  60.  
  61. this.userService.addUser(newUser);
  62.  
  63. console.log(JSON.stringify(newUser));
  64. }
  65. }
Add Comment
Please, Sign In to add comment