1

How can I show an element only if there are certain queryParams added to the url?

e.g.

ngOnInit() {
    this.route.queryParams.subscribe(params => {
        console.log(params);
    });
}

And I want to do something like:

<div *ngIf="queryParams = 'query_name'"></div>

2 Answers 2

3

You need to assign specific query param into a property and then use that property in the condition *ngIf.

queryParam;

ngOnInit() {
    this.route.queryParams.subscribe(params => {
        this.queryParam = params['yourParamName'];
    });
}

In the markup

<div *ngIf="queryParam == 'yourCondition'"></div>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but that way it is always present. What I mean is, if the query is passed, show something.
No, if query param is not taken, queryParam will be undefined and the condition will result false
Sorry, I was refreshing with the query in the url. Thank You very much!
0

in .ts file

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. For example if route is "/userId"

this.queryParams= params['userId'];

in .html file

<div *ngIf="queryParams.toLowerCase() === 'QUERY_NAME'.toLowerCase()"></div>

**For "QUERY_NAME" you can mention the intended query parameter string. For example if intended string is "userName"

 <div *ngIf="queryParams.toLowerCase() === 'userName'.toLowerCase()"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.