Angular 用角度插值选定参数id的最简单方法是什么?
我有一个网址:…/restaurants/4 我想显示所选id的值。在这种情况下,该id将是餐厅编号4 绑定餐厅参数id的最简单方法是什么Angular 用角度插值选定参数id的最简单方法是什么?,angular,Angular,我有一个网址:…/restaurants/4 我想显示所选id的值。在这种情况下,该id将是餐厅编号4 绑定餐厅参数id的最简单方法是什么 谢谢请查看示例代码: this.name = this.route.snapshot.queryParamMap.get("paramName") this.route.queryParamMap.subscribe(queryParams => { this.name = queryParams.get("paramName")
谢谢请查看示例代码:
this.name = this.route.snapshot.queryParamMap.get("paramName")
this.route.queryParamMap.subscribe(queryParams => {
this.name = queryParams.get("paramName")
})
这将为您提供参数值。在模板中使用{{param}}
希望这有帮助这是一个返回
id
的解决方案
import { Component, OnInit } from '@angular/core';
import { Router, NavigationStart } from '@angular/router';
constructor( private router: Router) {}
this.router.events.subscribe((event: Event) => {
if (event instanceof NavigationEnd ) {
const currentUrl = event.url;
// Get all characters after the last slash in url
const id = current.match(/[^\/]+$/)[0];
// id return 4 based on your provided link
}
});
我设法这样做了
export class AppComponent implements OnInit {
restaurant: Restaurant;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.paramMap.subscribe(params => {
this.id = (params.get('id'));
this.restaurant = restaurant[this.id];
});
}
在html中使用
{{restaurant.id}}
是的,我的目标是让这是我的html餐厅编号{{id}}
看看下面我的答案。代码返回id
。