asp.net-mvc - Angular 2 : how to get params from URL without using Routing?

标签 asp.net-mvc razor angular angular2-routing

我正在尝试将 URL 参数获取到我的组件中,以便使用 externsre 服务进行搜索。我读到我可以使用 angular2 路由并使用 RouteParams 获取参数,但我认为这不是我需要的,因为我的 Angular 应用程序的第一页是 Razor View 。

我会尽力解释得更好。 我的网址看起来像: http://dev.local/listing/?city=melbourne

我在 razor View 中加载我的根组件,如下所示:

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

然后在我的根组件上我有:

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);

    }
}

所以我期待我的组件中的“城市”获取从 razor View 传递的字符串,但它是未定义的。

我做错了什么?有没有办法在不使用路由的情况下获取参数?如果不是, Razor 的正确使用方法是什么?

最佳答案

Angular 不为此提供特殊支持。您可以使用 How can I get query string values in JavaScript?

据我所知,Location 计划进行重新设计,以提供对获取查询参数的支持,即使不使用路由器也是如此。

关于asp.net-mvc - Angular 2 : how to get params from URL without using Routing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36832258/

相关文章:

javascript - Paypal 点击按钮不适用于 safari

c# - 自定义字符串作为 Entity Framework 的主键

asp.net-mvc - 具有复杂类型 MVC 3 的自定义模型绑定(bind)器

javascript - 使用 foreach 使用 Razor 创建一个盒子并在其下方有一个数字的问题

asp.net-mvc - Web 项目需要缺少具有 Razor 语法 3.0.0.0 的 Web 组件 ASP.NET 网页

Angular4 Material sidenav 不工作

asp.net-mvc - Asp.Net MVC Controller 中的 Moq 假 cookie

c# - ViewBag 空空如也

asp.net - Views 文件夹外的 CSHTML 文件

javascript - 在 Angular 2 中显示从子组件到父组件的已检查 id