Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { Http, URLSearchParams, Response, RequestOptions, Headers } from '@angular/http';
- import 'rxjs/RX';
- import 'rxjs/add/operator/map';
- @Injectable()
- export class Service {
- apiRoot: string = 'https://jsonplaceholder.typicode.com/comments?'
- postId = 1;
- data;
- constructor(private _http: Http) {
- this._http.get(this.apiRoot).subscribe((data) => {
- this.data = data.json();
- });
- }
- init() {
- return this.data;
- }
- }
- import { Component, OnInit } from '@angular/core'; import { InfiniteService } from './infinite.service'; //import all rxjs/Rx opeartor import 'rxjs/add/operator/map';
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit {
- data;
- constructor(private _infinteService: InfiniteService) {
- this.data = this._infinteService.init();
- console.log(this.data);
- }
- GetPosition($event) {
- if ($event == 'Bottom') {
- console.log($event);
- }
- else {
- console.log($event);
- }
- }
- ngOnInit() {
- }
- }
- getData(): Observable<any> {
- return this._http.get(this.apiRoot)
- }
- this._infinteService.getData().subscribe(
- data => console.log(data)
- )
- //Subscribe your service in init() function and than call in your component
- import { Injectable } from '@angular/core';
- import { Http, URLSearchParams,Response, RequestOptions, Headers} from
- @angular/http';
- import 'rxjs/RX';
- import 'rxjs/add/operator/map';
- @Injectable()
- export class Service {
- apiRoot:string='https://jsonplaceholder.typicode.com/comments?
- postId=1';
- data;
- constructor(private _http:Http) {
- this.init();
- }
- init(){
- this._http.get(this.apiRoot).subscribe((data)=>{
- this.data= data.json();
- });
- return this.data;
- }
- }
Add Comment
Please, Sign In to add comment