A
A
Artem00712017-02-01 22:33:10
JavaScript
Artem0071, 2017-02-01 22:33:10

How to load data by routing parameter?

Good afternoon!
How to create a component that will take a parameter from the routing and send a get request relative to it?
For now, here's what's there:

import { Component, OnInit} from '@angular/core';
import { ActivatedRoute} from '@angular/router';
import { Response} from '@angular/http';
import { HttpService} from '../../services/http.service';

@Component({
    moduleId: module.id,
    selector: 'item-info',
    templateUrl: 'post.component.html',
    providers: [HttpService]
})
export class PostComponent implements OnInit {

    id;
    Post;
    constructor(private activateRoute: ActivatedRoute,private httpService: HttpService){}

    ngOnInit(){
        this.id = this.activateRoute.snapshot.params['id']; // получаем id из параметра в роутинге
        this.httpService.getOnePost(this.id).subscribe((data: Response) => this.Post=data.json()); // запарашиваем get запрос из сервиса
    }
}

HttpService
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';

@Injectable()
export class HttpService{
    constructor(private http: Http){ }
    getBlog(){
        return this.http.get('blog.json');
    }
    getOnePost(id){
        return this.http.get('blogOne.json');
    }
}

But it doesn't work.
How to make such a component, only a working one?)
Is there some example where both getting an id and sending a request?
Explanation of what doesn't work

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-02-02
@alexbraun

Show the implementation of HttpService
What writes to the stack trace?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question