Guest User

Untitled

a guest
May 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Http, URLSearchParams, Response, RequestOptions, Headers } from '@angular/http';
  3. import 'rxjs/RX';
  4. import 'rxjs/add/operator/map';
  5. @Injectable()
  6. export class Service {
  7. apiRoot: string = 'https://jsonplaceholder.typicode.com/comments?'
  8. postId = 1;
  9. data;
  10. constructor(private _http: Http) {
  11. this._http.get(this.apiRoot).subscribe((data) => {
  12. this.data = data.json();
  13. });
  14. }
  15. init() {
  16. return this.data;
  17. }
  18.  
  19. }
  20.  
  21. import { Component, OnInit } from '@angular/core'; import { InfiniteService } from './infinite.service'; //import all rxjs/Rx opeartor import 'rxjs/add/operator/map';
  22.  
  23. @Component({
  24. selector: 'app-root',
  25. templateUrl: './app.component.html',
  26. styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit {
  27. data;
  28. constructor(private _infinteService: InfiniteService) {
  29. this.data = this._infinteService.init();
  30. console.log(this.data);
  31. }
  32.  
  33. GetPosition($event) {
  34. if ($event == 'Bottom') {
  35. console.log($event);
  36. }
  37. else {
  38. console.log($event);
  39. }
  40. }
  41. ngOnInit() {
  42.  
  43.  
  44. }
  45.  
  46. }
  47.  
  48. getData(): Observable<any> {
  49. return this._http.get(this.apiRoot)
  50. }
  51.  
  52. this._infinteService.getData().subscribe(
  53. data => console.log(data)
  54. )
  55.  
  56. //Subscribe your service in init() function and than call in your component
  57.  
  58. import { Injectable } from '@angular/core';
  59. import { Http, URLSearchParams,Response, RequestOptions, Headers} from
  60. @angular/http';
  61. import 'rxjs/RX';
  62. import 'rxjs/add/operator/map';
  63. @Injectable()
  64. export class Service {
  65. apiRoot:string='https://jsonplaceholder.typicode.com/comments?
  66. postId=1';
  67. data;
  68. constructor(private _http:Http) {
  69. this.init();
  70. }
  71. init(){
  72. this._http.get(this.apiRoot).subscribe((data)=>{
  73. this.data= data.json();
  74. });
  75. return this.data;
  76. }
  77.  
  78. }
Add Comment
Please, Sign In to add comment