0

I have an application written in Angular 13 using Typescript.

I want to hide items with get url parameters and set it to true if it doesn't exist.

export class AppComponent {

  menu: boolean = true;

  constructor(private oauthService: InitialAuthService, private userRoleService: UserRoleService, private route: ActivatedRoute,) {
  }

  ngOnInit(): void {

    this.route.queryParams
      .subscribe((params: any) => {

        this.menu = !!params["menu"] ? params["menu"] : true

      }
    );
    this.menu = this.route.snapshot.queryParams['menu'];

I simply want to cache HTML element following parameters obtained in URL.

For example

http://localhost:4200/index?menu=false   // hide menu 
http://localhost:4200/index?menu=true    // show menu 
http://localhost:4200/index              // show menu 

html side

<div *ngIf="menu">...</div>
6
  • Can you clarify the problem? What exactly is not working as you expect? Commented Jan 16, 2023 at 15:47
  • Please provide more information. Is something not working as expected, or what's the problem? Commented Jan 16, 2023 at 15:47
  • it's more clearer Commented Jan 16, 2023 at 16:08
  • Does this answer your question? Angular query params as boolean type, not string Commented Jan 16, 2023 at 16:19
  • it's not problem of String Commented Jan 16, 2023 at 16:32

1 Answer 1

1

Try this one:

export class AppComponent implements OnInit {

  isShown$!: Observable<boolean>;

  constructor(private _route: ActivatedRoute) {}

  ngOnInit(): void {
    this._handleRouteQueryParams();    
  }

  private _handleRouteQueryParams(): void {
    this.isShown$ = this.route
      .queryParamMap
      .pipe(map(params => {
        return !params.has('menu') || params.get('menu') === 'true';
    }));
  }
}

In the template

<div *ngIf="isShown$ | async">...</div>
Sign up to request clarification or add additional context in comments.

1 Comment

think it's work

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.