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

Mario Lopez

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?

Günter Zöchbauer

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get query params from url in Angular 2?

Get route params from url

How to achieve angular2 nested component routing without the additional slash in url path

Retrieve data from url wiith params in angular2

How to Unit Test Angular 2 routing params

How to create Singleton service in Angular 2 and read URL Params

How to get parameters using "/:params" in Angular 2

Get activated route from angular 2 routing

Angular2+ routing - optional route params

Angular2+ | How to define routing with params in NgModule

Angular 2 routing: prevent URL from changing

How to get parameter from URL(Routing) Angular 2?

How to display a seperate html page without using a component for routing in angular 2?

How to get Params from url for Axios GET request?

how get react router origin url without params?

How to include a param in browsers url when using params instead of url in angular ui routing?

How to store add params in api url using angular js

How to get a URL using routing tables from a Service Class?

Remove URL params on routing

How to get URL parameters in Angular2 routing code written in ES5?

Angular2 http get request with Observables and dynamic url params. How to?

How to remove a redirect of a URL from Angular Routing

How to get the URL from a localhost server without using libcurl

Angular routing using %2F in URL which is breaking routing

Angular 11 get params from URL

How to get id from the url react for dispatch instead of using params?

How to get items without specific params from MongoDB using Mongoose (NodeJS)

Unable to get id from the URL using params

How to get the angular route params values in the url?