Advertisement
Guest User

Untitled

a guest
Jul 13th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. import { Component, EventEmitter, OnInit, Output } from '@angular/core';
  2. import { FormGroup, FormBuilder, Validators } from '@angular/forms';
  3.  
  4. import { Post } from './model/post';
  5. import { PostService } from './service/post.service';
  6.  
  7. @Component({
  8. selector: 'setup',
  9. templateUrl: './setup.component.html',
  10. styleUrls: ['./setup.component.scss']
  11. })
  12. export class SetupComponent implements OnInit {
  13.  
  14. @Output()
  15. change: EventEmitter<string> = new EventEmitter();
  16.  
  17. postUsers(input){
  18. this.postService.postUser(input)
  19. .subscribe(
  20. post => {
  21. this.post = post
  22. },
  23. err => {
  24. console.log(err);
  25. });
  26. }
  27.  
  28. clicked(value) {
  29. console.log(value);
  30. this.postUsers(this.input)
  31. // this.change.emit(value);
  32. }
  33.  
  34. complexForm : FormGroup;
  35. constructor(private postService: PostService) {}
  36.  
  37. post: Post[];
  38.  
  39. ngOnInit() {}
  40.  
  41. }
  42.  
  43. import { Injectable } from '@angular/core';
  44. import { Http, Response, Headers, RequestOptions } from '@angular/http';
  45. import { Post } from '../model/post';
  46. import { Observable } from 'rxjs/Rx';
  47.  
  48. // Import RxJs required methods
  49. import 'rxjs/add/operator/map';
  50. import 'rxjs/add/operator/catch';
  51.  
  52. @Injectable()
  53. export class PostService {
  54.  
  55. constructor (private http: Http) {}
  56. private registerUrl = 'http://localhost:2001/api/users/register';
  57.  
  58. postUser(user:Post) : Observable<Post[]>{
  59. let headers = new Headers({ 'Content-Type': 'application/json' });
  60. let options = new RequestOptions({ headers: headers });
  61. return this.http.post(this.registerUrl, user, options)
  62. .map(this.extractData)
  63. .catch(this.handleError);
  64. }
  65.  
  66. private extractData(res: Response) {
  67. let body = res.json();
  68. return body || { };
  69. }
  70. private handleError (error: Response | any) {
  71. // In a real world app, you might use a remote logging infrastructure
  72. let errMsg: string;
  73. if (error instanceof Response) {
  74. const body = error.json() || '';
  75. const err = body.error || JSON.stringify(body);
  76. errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
  77. } else {
  78. errMsg = error.message ? error.message : error.toString();
  79. }
  80. console.error(errMsg);
  81. return Observable.throw(errMsg);
  82. }
  83.  
  84. }
  85.  
  86. export class Post {
  87. constructor(
  88. public id: number,
  89. public name: string,
  90. public email: string,
  91. public password: string
  92. ){}
  93. }
  94.  
  95. this.postUsers(this.input)
  96. .then(function(){
  97. });
  98.  
  99. clicked(value) {
  100. console.log(value);
  101. this.postUsers(this.input)
  102. // this.change.emit(value);
  103. }
  104.  
  105. clicked(value) {
  106. console.log(value);
  107. this.postUsers(this.input)
  108. .then(function(){
  109. this.change.emit(value);
  110. });
  111. }
  112.  
  113. clicked(value) {
  114. console.log(value);
  115. const runThis = this.postUsers(this.input);
  116. console.log(runThis);
  117. // this.change.emit(value);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement