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?

Mario Lopez
  • 1,405
  • 13
  • 24

1 Answers1

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.

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    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? – Mario Lopez Apr 25 '16 at 23:23