【发布时间】:2020-01-12 10:29:20
【问题描述】:
在 PHP 中,您将获得所有 URL 参数
print_r($_GET);
在 JavaScript 中是这样的
window.onload = function() {
GetURLParameter();
};
function GetURLParameter()
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
var oldValue = document.getElementById("result").value;
var newValue = oldValue + '\n' + sParameterName[0] + ': ' + sParameterName[1];
document.getElementById("result").value = newValue;
}
}
在 Angular 中?
我看到的所有示例都查询某个参数。您必须提前知道参数。例如
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'url-call';
paramList: string[] = [];
param1: string;
constructor(private activatedRoute: ActivatedRoute) { }
ngOnInit() {
console.log(this.activatedRoute.snapshot.queryParamMap.get("param1"));
this.activatedRoute.queryParamMap.subscribe(queryParams => {
console.log(queryParams.get("param1"));
})
}
}
如何在不知道参数名称的情况下打印出所有/任何参数?
另外queryParamMap和queryParams有什么区别?
【问题讨论】:
标签: angular angular7 url-parameters