Guest User

Untitled

a guest
Dec 14th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. export class User {
  2. username: string;
  3. password: string;
  4. constructor(){}
  5. }
  6.  
  7. import { BrowserModule } from '@angular/platform-browser';
  8. import { NgModule } from '@angular/core';
  9. import { FormsModule } from '@angular/forms';
  10. import { HttpModule } from '@angular/http';
  11.  
  12. import { AppComponent } from './app.component';
  13. import { UserService } from './user.service';
  14.  
  15.  
  16. @NgModule({
  17. declarations: [
  18. AppComponent
  19. ],
  20. imports: [
  21. BrowserModule,
  22. FormsModule,
  23. HttpModule
  24. ],
  25. providers: [UserService],
  26. bootstrap: [AppComponent]
  27. })
  28. export class AppModule { }
  29.  
  30. import { Injectable } from '@angular/core';
  31. import { Http, Response, Headers, RequestOptions } from '@angular/http';
  32. import 'rxjs/Rx';
  33. import { Observable } from 'rxjs/Observable';
  34.  
  35. import { User } from './user';
  36.  
  37. @Injectable()
  38. export class UserService {
  39.  
  40. private baseUrl: string='http://localhost:8080/api';
  41. private headers = new Headers({'Content-Type':'application/json'});
  42. private options = new RequestOptions({headers:this.headers});
  43. private user: User;
  44.  
  45. constructor(private _http: Http) { }
  46.  
  47. getUsers() {
  48. return this._http.get(this.baseUrl + '/users', this.options)
  49. .map((response: Response) => response.json())
  50. .catch(this.errorHandler);
  51. }
  52.  
  53. errorHandler(){
  54. return Observable.throw(Error || "!!! SERVER ERROR !!!");
  55. }
  56. }
  57.  
  58. import { Component, OnInit } from '@angular/core';
  59.  
  60. import { UserService } from './user.service';
  61. import { User } from './user';
  62.  
  63. @Component({
  64. selector: 'app-root',
  65. templateUrl: './app.component.html'
  66. })
  67. export class AppComponent implements OnInit {
  68. public users: User[];
  69.  
  70. constructor(private _userService: UserService) {}
  71.  
  72. ngOnInit() {
  73. this._userService.getUsers().subscribe((users)=>{
  74. this.users=users;
  75. }, (error)=>{
  76. console.log(error);
  77. })
  78. }
  79. }
  80.  
  81. <td class="pt-4">{{user?.username}}</td>
  82. <td class="pt-4">{{user?.password}}</td>
Add Comment
Please, Sign In to add comment