5

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?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Günter, for some reason I forgot I can use just plain javascript. That made the trick.In regards to the parameter being passed to the root component I couldn't make it work, do you know if that is even possible? I assumed that because that tag is not part of an Angular template it is not processed by Angular at all but just to replace it with the proper root component?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.