Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

angular 13 viewchild

import { 
    Component, 
    OnInit, 
    ElementRef, 
    ViewChild, 
    OnDestroy, 
    ChangeDetectionStrategy, 
    ChangeDetectorRef } from '@angular/core';

import { Router } from '@angular/router';
import { User } from 'src/app/common/auth/user';
import { AuthService } from 'src/app/common/services/auth.service';
import { MatSnackBar } from '@angular/material/snack-bar';
import { takeUntil } from 'rxjs/operators';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class HeaderComponent implements OnInit, OnDestroy {
  @ViewChild('searchBar') searchBar: ElementRef | null = null;
  public signedIn: boolean = false;
  public authUser: User | null = null;
  private destroyNotifier$: Subject<boolean> = new Subject<boolean>();
  constructor(
    public authService: AuthService,
    private router: Router, 
    private snackBar: MatSnackBar, 
    private changeDetectorRef: ChangeDetectorRef) { }

  ngOnInit(): void {
    this.authService.authState
    .pipe(takeUntil(this.destroyNotifier$))
    .subscribe({
      next: (authState: AuthState) => {
        this.signedIn = authState.signedIn;
        this.authUser = authState.currentUser;
        this.changeDetectorRef.markForCheck();
      }
    });
  }
  ngOnDestroy(): void {
    this.destroyNotifier$.next(true);
    this.destroyNotifier$.complete();
  }
  logOut(): void {
    this.authService.logOut();
    this.router.navigateByUrl('/users/login');
  }
  findUsers(): void {
    const searchText = this.searchBar?.nativeElement.value;
    if(searchText == ''){
      this.snackBar.open('Enter a search text', 'Ok', {
        duration: 5 * 1000
      });
      return;
    }
    const result = this.authService.findUsers(searchText);
    console.log(result);
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: disable sonar rule in code 
Typescript :: Make Object properties Readonly TypeScript 
Typescript :: how ro execute typescript file 
Typescript :: Make Array Consecutive 2 
Typescript :: prisma user model 
Typescript :: form reset typescript 
Typescript :: group elements in list with some attributes 
Typescript :: check if name is unique among non-deleted items laravel 
Typescript :: axios typescript 
Typescript :: typerscript online compiler 
Typescript :: nested slots in vue 
Typescript :: how to get value from observable 
Typescript :: google sheets mode text 
Typescript :: typescript compile on save 
Typescript :: why in angular template i cant use Object.Keys() 
Typescript :: check if column exists in dataframe python 
Typescript :: typescript generic dictionary 
Typescript :: select constraints in sql 
Typescript :: basic variable typescript 
Typescript :: how to restrict alphabets in input field in angular 
Typescript :: filename requests python 
Typescript :: selenium multiple elements with same class name python 
Typescript :: execute script when c# code gets executed 
Typescript :: how to set date axes limits in matplotlib plot 
Typescript :: check type of object typescript 
Typescript :: intrinsicattributes typescript 
Typescript :: paragraph dots after 2 lines css 
Typescript :: pytest tests in subfolder 
Typescript :: what is the importance of testng xml file 
Typescript :: custom events in unity c# 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =