Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
Passing void events from parent component to child component

Quick overview

I have a "Terminal" Component which should be able to be used multiple times all over my application. This component should also be able to put into a "read only" mode where you pass a single command you'd like the terminal to fire and it will display the output. I am trying to have this component be able to be refreshed by many things; data updating else where, user events, etc. To achieve this, currently, I am using RxJS subjects as an @Input into the terminal component which when updated fires the subscribed functions. This works for the first user click (see bellow) but after that the subject doesn't update again. I suspect this is due to the "object" not updating, there for angular doesn't register the change and my whole idea falls apart.

Can I fix this? or do I need to redesign this "Terminal" component?

Code

terminal.component.ts

export class TerminalComponent implements OnInit, OnDestroy {
    constructor() {}

    $destroy = new Subject();
    terminalOutput = '';

    // Command input (if you want the terminal to only fire one command)
    @Input() command = '';
    $command: BehaviorSubject<string> = new BehaviorSubject('');

    // Refresh terminal input
    $refresh: Subject<void> = new Subject();
    @Input() set refresh(value: Subject<void>) {
        this.$refresh = value;
    }

    // ReadOnly Input
    @Input() readOnly = false;

    ngOnInit(): void {
        this.$refresh
            .pipe(
                takeUntil(this.$destroy),
                tap(() => {
                    const lastCommand = this.$command.getValue();
                    if (lastCommand) {
                        console.log('Refreshing, last command is:', lastCommand);                       
                    }
                })
            )
            .subscribe();

        //...
    }

    //...
}

parent.component.html

<h1>Home</h1>

<app-terminal command="ls" [refresh]="$refreshSubject"></app-terminal>

<button (click)="refreshTest()">Refresh</button>

parent.component.ts

export class ParentComponent implements OnInit {
    $refreshSubject: Subject<void> = new Subject();

    constructor() {}

    ngOnInit(): void {}

    refreshTest(): void {
        console.log('Refreshing');
        this.$refreshSubject.next();
    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So I found the problem, it was another bug in my code that was causing the RxJS tap to not fire after the first time.

 ngOnInit(): void {
        this.$refresh
            .pipe(
                takeUntil(this.$destroy),
                tap(() => {
                    const lastCommand = this.$command.getValue();
                    if (lastCommand) {
                        console.log('Refreshing, last command is:', lastCommand);                       
                        throw new Error("Example Error"); 
                        // ^^^^ This will cause future Subject emits to not fire as this function has failed.
                    }
                })
            )
            .subscribe();

        //...
    }

Side note: I think the question title should be changed to better suit the real problem with my code, and there for have better SEO. However if this is a completely rubbish question then I think it should be deleted.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!