[英]angular - *ngIf conditional based on url params
How can I show an element only if there are certain queryParams
added to the url? 仅当URL中添加了某些
queryParams
才如何显示元素?
eg 例如
ngOnInit() {
this.route.queryParams.subscribe(params => {
console.log(params);
});
}
And I want to do something like: 我想做类似的事情:
<div *ngIf="queryParams = 'query_name'"></div>
You need to assign specific query param into a property and then use that property in the condition *ngIf
. 您需要将特定的查询参数分配给一个属性,然后在条件
*ngIf
使用该属性。
queryParam;
ngOnInit() {
this.route.queryParams.subscribe(params => {
this.queryParam = params['yourParamName'];
});
}
In the markup 在标记中
<div *ngIf="queryParam == 'yourCondition'"></div>
in .ts file 在.ts文件中
private queryParams:string ='';
ngOnInit() {
this.route.queryParams.subscribe(params => {
this.queryParams= params['PARAMETER_NAME'];
});
}
**For "PARAMETER_NAME" you can mention the name of the query parameter. **对于“ PARAMETER_NAME”,您可以提及查询参数的名称。 For example if route is "/userId"
例如,如果route是“ / userId”
this.queryParams= params['userId'];
in .html file 在.html文件中
<div *ngIf="queryParams.toLowerCase() === 'QUERY_NAME'.toLowerCase()"></div>
**For "QUERY_NAME" you can mention the intended query parameter string. **对于“ QUERY_NAME”,您可以提及预期的查询参数字符串。 For example if intended string is "userName"
例如,如果预期的字符串是“ userName”
<div *ngIf="queryParams.toLowerCase() === 'userName'.toLowerCase()"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.