Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

Angular 12: Trigger multiple child components at once

//Service

@Injectable({
  providedIn: 'root',
})
export class DoSomethingService {
  subject = new Subject<void>();
}

//Parent

export class AppComponent {
  constructor(private doSomethingService: DoSomethingService) {}

  makeSomethingHappen() {
    this.doSomethingService.subject.next();
  }
}
<button (click)="makeSomethingHappen()">CLICK ME</button>

<app-one></app-one>
<app-two></app-two>
<app-three></app-three>

//Children

export class OneComponent implements OnInit {
  message = 'Component one standing by...';
  sub = new Subscription();

  constructor(private doSomethingService: DoSomethingService) {}

  ngOnInit() {
    this.sub = this.doSomethingService.subject.subscribe(() =>
      this.doSomething()
    );
  }

  doSomething() {
    this.message = 'Component one did something!';
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: how to add typescript tp create react app 
Typescript :: Exclude code from hints delphi 7 
Typescript :: which is the best it company for freshers 
Typescript :: typescript array of mixed type 
Typescript :: dto typescript 
Typescript :: Link renders blank page 
Typescript :: surround substring with quotes 
Typescript :: typeorm versioncolumn 
Typescript :: java concepts mcq 
Typescript :: develop an algorithm that prints 2 numbers so that one is a multiple of the other 
Typescript :: edit lights in a room alexa 
Typescript :: how do you check ewhich version of typescript you are using 
Typescript :: ts types passing functions 
Typescript :: render html tags in typescript 
Typescript :: ValueError: Not all points are within the bounds of the space. 
Typescript :: e.target.value typescript 
Typescript :: Decrypt 
Typescript :: how to implement read more and readless in angular 
Typescript :: download objects under a prefix in golang 
Typescript :: How to check that tuple A contains all elements of tuple B python? 
Typescript :: global hotkeys typescript react 
Typescript :: TypeScript interface for object with arbitrary numeric property names? 
Typescript :: fieldmatch cannot be resolved to a type 
Typescript :: centos remote desktop clients vs remote management for linux 
Typescript :: testing with limited information 
Typescript :: github actions typescript 
Typescript :: sarasota bowling alley bomb threats incident 
Typescript :: splice array based on index typescript 
Typescript :: google sheets past tsv data 
Typescript :: how to concate a string to all elements in a list in python 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =