Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. My app.component.ts
  2. //app.components.ts
  3.  
  4. import { Component } from '@angular/core';
  5. import {User} from './user';
  6. @Component({
  7. selector: 'app-root',
  8. templateUrl: './app.component.html',
  9. styleUrls: ['./app.component.css']
  10. })
  11.  
  12. export class AppComponent {
  13. title = 'Society CRM';
  14. }
  15.  
  16. //user.ts
  17. export class User
  18. {
  19. _id : string;
  20. name: string;
  21. email: string;
  22. username: string;
  23. password: string;
  24. phone: string;
  25. address: string;
  26. role: string;
  27. created_date: string;
  28. }
  29.  
  30. //user-list.component.ts
  31. My user list component file
  32.  
  33. import {Component,OnInit} from '@angular/core';
  34. import {User} from './user';
  35. import {UserService} from './user.service';
  36. import { Headers, RequestOptions } from '@angular/http';
  37. @Component({
  38. selector:'user-list',
  39. templateUrl:'./user-list.html',
  40. providers: [ UserService ]
  41. })
  42.  
  43. export class UserListComponent implements OnInit{
  44. errorMessage: string;
  45. users: User[];
  46. mode = 'Observable';
  47. constructor (private userService: UserService) {};
  48. ngOnInit() {
  49. this.getUsersList();
  50. alert('here');
  51. }
  52. getUsersList() {
  53. return this.userService.getUsersList()
  54. .subscribe(
  55. users => this.users = users,
  56. error => this.errorMessage = <any>error);
  57. }
  58. }
  59.  
  60. //app.module.ts
  61. My app.module.ts
  62.  
  63. import { BrowserModule } from '@angular/platform-browser';
  64. import { NgModule } from '@angular/core';
  65. import { HttpModule } from '@angular/http';
  66. import { AppComponent } from './app.component';
  67. import { UserListComponent } from './user-list.component';
  68. import { UserService } from './user.service';
  69. import { FormsModule } from '@angular/forms'; // <-- NgModel lives here
  70.  
  71. @NgModule({
  72. declarations: [
  73. AppComponent,
  74. UserListComponent
  75. ],
  76. imports: [
  77. BrowserModule,
  78. HttpModule,
  79. FormsModule
  80. ],
  81. providers: [UserService],
  82. bootstrap: [AppComponent]
  83. })
  84. export class AppModule { }
  85.  
  86. user.service.ts
  87. My services.ts file
  88.  
  89. import {Injectable} from '@angular/core';
  90. import {Http, Response} from '@angular/http';
  91. import {Observable} from 'rxjs/Observable';
  92. import 'rxjs/add/operator/map';
  93. import 'rxjs/add/observable/throw';
  94. import 'rxjs/add/operator/catch';
  95. import {User} from './user';
  96. import { Headers, RequestOptions } from '@angular/http';
  97. @Injectable()
  98. export class UserService
  99. {
  100. constructor (private http:Http){}
  101. private api_url = "http://localhost:3000/api/";
  102. getUsersList() : Observable<User[]>
  103. {
  104. let headers = new Headers();
  105. headers.append('Access-Control-Allow-Origin', '*');
  106. headers.append('Access-Control-Allow-Credentials', 'true');
  107. headers.append('Access-Control-Allow-Methods', 'GET');
  108. headers.append('Access-Control-Allow-Headers', 'Content-Type');
  109. let options = new RequestOptions({headers: headers});
  110. return this.http.get(this.api_url+'getUsersList',options)
  111. .map(this.extractData)
  112. .catch(this.handleError);
  113.  
  114. }
  115.  
  116. private extractData(res: Response) {
  117. let body = res.json();
  118. return body.data || {};
  119. }
  120. private handleError(error: Response | any) {
  121. // In a real world app, you might use a remote logging infrastructure
  122. let errMsg: string;
  123. if (error instanceof Response) {
  124. const body = error.json() || '';
  125. const err = body.error || JSON.stringify(body);
  126. errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
  127. } else {
  128. errMsg = error.message ? error.message : error.toString();
  129. }
  130. console.error(errMsg);
  131. return Observable.throw(errMsg);
  132. }
  133. }
  134.  
  135. I don't know what exactly is happening with the above code, no error being shown on firebug console or browser console. Any help would e thankful
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement