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

283
Views
How to subscribe to input for it to save changes into an object?

I have an input in a form of HTML like this:

 <mat-form-field appearance="fill">
    <mat-label>Flowrate: </mat-label>
    <input id = "flowRate" type="number" matInput>
  </mat-form-field>

To which I subscribe later in my .ts file like this:

private flowRateInput: any = document.getElementById('flowRate') as HTMLInputElement

 if (flowRateInput: any) {
  let flowObs = fromEvent(this.flowRateInput, 'input').pipe(
      debounceTime(500)
    ).subscribe(res => {
      this.calcData.flowRate = +this.flowRateInput.value;
      console.log(this.flowRateInput.value)
      console.log(this.calcData)
    })
  }

However it seems to do nothing when I open the page and change the input. I have a guess that I did something wrong in subscribe part of the code.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Try something like this, using FormControl :

.html file:

<mat-form-field appearance="fill">
  <mat-label>Flowrate: </mat-label>
  <input id="flowRate" type="number" [formControl]="fcFlowRate" matInput>
</mat-form-field>

.ts file :

// Before constructor
fcFlowRate = new FormControl();

// In NgOnInit
this.fcFlowRate.valueChanges.pipe(debounceTime(500)).subscribe( value => {
  this.calcData.flowRate = +value;
  console.log(value)
  console.log(this.calcData)
})
about 4 years ago · Juan Pablo Isaza Report

0

you don't need to subscript and you don't need to to do document.getElementById('flowRate') in angular.

you can use ngModel for example: <input type="number" matInput [(ngModel)]="flowRate">

private _flowRate="";
set flowRate(flowRate){
 this._flowRate = flowRate;
  this.calcData.flowRate = +this.flowRateInput.value;
}
get flowRate(){
 return this._flowRate;
}

or you can listen to the change event: <input type="number" matInput (change)="flowRateChange($event)> or <input type="number" matInput [ngModel]="flowRate" (ngModelChange)="flowRateChange($event)"

about 4 years ago · Juan Pablo Isaza Report

0

Using document.getElementById('flowRate') is discouraged when you are using Angular, instead, you should template-driven forms or reactive-forms, which is the angular way of doing so.

I'll show the reactive-form example

First import the ReactiveFormsModule in your app.module.ts, so that FormControls work in your example.

<mat-form-field appearance="fill">
    <mat-label>Flowrate: </mat-label>
    <input [formControl]="flowRateControl" type="number" matInput>
</mat-form-field>

In your .ts component

 flowRateControl: FormControl = new FormControl();

 ngOnInit(): void {
    this.flowRateControl.valueChanges.subscribe(val => {
            console.log(val);
       });
  }

Also, the benefits of using this approach are that you can use multiple RxJs operators like debounce, distinctUntilChanged which can boost your user experience.

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!