Mario Lopez
Mario Lopez

Reputation: 1465

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

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?

Upvotes: 5

Views: 2764

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657078

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.

Upvotes: 4

Related Questions