Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: how to get params from URL without using Routing?

Tags:

I'm trying to get the URL parameters into my component to search using an externsre service. I read I can use angular2 routes and get params using RouteParams but I don't think that's what I need because the first page of my angular application is a Razor view.

I'll try to explain better. My URL looks like: http://dev.local/listing/?city=melbourne

I'm loading my root component in a razor view with something like:

<house-list city = "@Request.Params["city"]"></house-list>

then on my root component I have:

export class AppComponent implements OnInit {
    constructor(private _cityService: CityService) { }

    response: any;
    @Input() city: any;

    ngOnInit() {
        this.getHouses();
    }

    getHouses() {
        this.__cityService.getHousesByCity(this.city)
            .subscribe(data => this.response = data);

    }
}

So I was expecting that the 'city' in my component gets the string passed from the razor view but it's undefined.

What am I doing wrong? Is there a way of getting the params without using the routing? If not, what is the right way of using razor?

like image 398
Mario Lopez Avatar asked Apr 25 '16 04:04

Mario Lopez


1 Answers

Angular doesn't provide special support for this. You can use How can I get query string values in JavaScript?

From what I have seen Location is planned to be reworked to provide support for get query params even without using the router.

like image 51
Günter Zöchbauer Avatar answered Sep 28 '22 04:09

Günter Zöchbauer