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

222
Views
How to get the value of a FormControl in angular2

I have the following form with an FormArray containing FormGroups

 <form [formGroup]="screenForm" novalidate>

  <div formArrayName="iPhoneScreenshots" class="row">
    <div *ngIf="screenForm.controls.iPhoneScreenshots?.length > 0">
      {{screenForm.controls.iPhoneScreenshots?.length }}
      <div *ngFor="let url of screenForm.controls.iPhoneScreenshots.controls; let i=index">
        <div [formGroupName]="i">
          <input class="form-control" formControlName="url">
          <img src="{{app.screenshotUrls[i]}}" class="rounded img-fluid app-screen" style="height:200px"/>
        </div>
      </div>
    </div>
  </div>
</form>

the url values come from an array which is getting populated via API in the callback I set the values:

private setScreenShots(app: ItunesApp): void {
if (app.hasOwnProperty('screenshotUrls')) {
  const screenShots = app.screenshotUrls.map(url => {
      return this.fb.group({
        url: [url, Validators.required]
      });
    }
  );
  const screenShotsArray = this.fb.array(screenShots);
  this.screenForm.setControl('iPhoneScreenshots', screenShotsArray);
 }
}

initial array is empty

 private createForm() {
   this.appSiteForm = this.fb.group({
  title: ['', Validators.required]
});

this.screenForm = this.fb.group({
  iPhoneScreenshots: this.fb.array([]),
});

}

this code line looks very strange to me

img src="{{app.screenshotUrls[i]}}" 

I am doing this since url.value() or url.get('value') raises errors.

So how can I access the value of a form control?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can try to get control from url group

<div *ngFor="let urlGroup of screenForm.controls.iPhoneScreenshots.controls; let i=index">
    <div [formGroupName]="i">
        <input class="form-control" formControlName="url">
        <img [src]="urlGroup.get('url').value" />
    </div>
</div>

Plunker Example

about 4 years ago · Santiago Trujillo Report

0

Try this:

screenForm.get('url').value

If this doesn't work, I'll have to see your entire form builder to assist further.

about 4 years ago · Santiago Trujillo 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!