Uploading Files through Reactive Forms in Angular

I tried following a tutorial on integrating file upload functionality into my reactive form, which can be found at the following link: . However, I've encountered an issue where I'm getting an error message stating "this.onChange is not a function". Here's a snippet of my Component:

@Component({
  selector: 'first-page',
  templateUrl: './first-page.component.html',
  styleUrls: ['./first-page.component.css'],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: FirstPageComponent,
      multi: true
    }
  ]
})
 @Input()progress;
  onChange:Function;
  private file:File|null=null;

  @HostListener('change',['$event.target.files']) emitFiles(event:FileList){
    const file= event && event.item(0);
    this.onChange(file);
    this.file=file;
  }
  writeValue(value:null){
    //clear file input
    this.host.nativeElement.value='';
    this.file=null;
  }
  registerOnChange(fn: Function){
    this.onChange = fn;
  }
  registerOnTouched(fn: Function){
  }

  
  ngOnInit(){
  [remaining content goes here...] 
  }
  onInputChange(event: MatSliderChange){
    this.myValue=event.value;
  }
  public checkErrorP=(controlName:string, errorName:string)=>{
    return this.profileForm.controls[controlName].hasError(errorName);
  }
  [more Check Error functions...]
  addSkill(){
    //this.conoscenze.push((<HTMLInputElement>document.getElementById("knowledge")).value);
    this.knowledgeForm.value.conoscenzeForm.push(new FormControl({name: this.conoscenze[this.i] , value:this.myValue}));
    this.i++;}

I'm struggling to pinpoint the exact cause of the error. Can anyone suggest an alternative solution for implementing file uploads via reactive forms? (Originally, I intended to follow the tutorial for implementing 2 file uploads)

Answer №1

It turns out @MikeOne was correct - I realized the importance of separating the component that houses all the controlValueAccessor.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Node.js interacting with a MySQL database to create a RESTful API

I am attempting to utilize query parameters with MySQL and NodeJS, but I am not receiving any response in the console. Instead, I am getting all students' details as output. app.get('/api/students', (req, res) => { const name = req.query ...

Looking to create a dynamic Angular reactive form using API response data? Seeking guidance on how to achieve this? Let's

[ { "name": "jkjk", "firstName": "hgh", "lastName": "ehtrh", "replytype": "svdv", "prodCode": "svv", "execu ...

Error: NPM link - Application unable to detect dependencies of the linked library in Angular

Currently, I am working on an application along with a library that is used within the application. The issue I am facing is related to updating the library using NPM link. It seems that when I import the library into the application, the required dependen ...

The issue of the mat-select change event failing to trigger in an Angular 13 reactive form when using the

How to use mat-select within a form-group <mat-form-field appearance="outline"> <mat-select formControlName="formula" id="formula"> <mat-option [value]="metricFormula.TotalCount">{{l(&apos ...

Different ways to determine if a given string exists within an Object

I have an object called menu which is of the type IMenu. let menu: IMenu[] = [ {restaurant : "KFC", dish:[{name: "burger", price: "1$"}, {name: "french fries", price: "2$"}, {name: "hot dog", d ...

Adjust the design of your Angular 2/5 app based on configuration file dynamically

I'm currently exploring the concept of a flexible layout that can be customized by users. Here's how it works: By default, there is a set layout of elements on the page (such as inputs, tables, etc). The user has the ability to rearrange where ...

Tips for customizing the font color in Material UI Typography

Is it possible to change the color of only this text to red? return <Typography style={{ color: 'red' }}>Login Invalid</Typography> I came across this online solution, but I am unsure how to implement it as there is no theme={color ...

Issues with Injectable Service within Another Service in Angular 2

Having a problem with injecting a service into another service. I have a ContactService that retrieves data from the server using the handleError method, and an AlertService that handles errors thrown from the handleError method. Both services are declared ...

Having trouble getting Typescript code to function properly when using commonjs style require statements

I am completely new to Typescript, Node.js, and Express. Following the instructions outlined in this tutorial (https://www.digitalocean.com/community/tutorials/setting-up-a-node-project-with-typescript), I set up my project exactly as described there. The ...

Does Apollo Federation provide support for a code-first development approach?

I have declarations using 'code-first' approach in my project, but now I want to utilize them as microservices. How can I separate my 'typeDefs' and 'resolvers' following Apollo's 'schema-first' methodology? Is ...

Why is my Angular proxy failing to rewrite the path when making an HTTP GET request?

I am facing an issue with my proxy configuration where it is not redirecting as expected based on the rewrite configuration. You can find my proxy.config.json below: { "/sap": { "target" : "http://server.domain.com:8002", "secure" : fa ...

The RazorPay callback encountered an Uncaught TypeError indicating that the function is not recognized

In my TypeScript class, I have a defined handler as follows: "handler": function (response) { this.sendUserStatus(); }, Unfortunately, when I attempt to call this.sendUserStatus();, I encounter the following error: Uncaught Typ ...

Utilizing Array in ReactJS with the Power of Hooks

My current situation involves having an array of FooBar objects interface FooBar { foo: string, bar: string, correct: string, other: string[] } const [arrOfObj, setArrOfObj] = useState<FooBar[]>([ { "foo": "fool ...

Creating a Dynamic Download Link in Angular 6

Hello everyone, I need assistance in making my download feature dynamic. Here is my HTML code: <button class="btn btn-primary" (click)="downloadMyFile({{account.students[0].schoolId}})"><i class="fa fa-file-pdf-o"></i> Download</butt ...

Issue: NG0204: Unable to find solutions for all parameters in NzModalRef: (?, ?, ?)

I'm currently working on an Angular project utilizing the NZ-Zorro library, and I'm encountering difficulty understanding which parameters are causing issues with NzModalRef when attempting to run the test code coverage. The error message display ...

Show a Pair of Images Upon Submission Utilizing Ajax

Imagine having two separate div containers displayed as shown below: https://i.sstatic.net/qQwjV.png What I achieved was clicking the submit button would upload the image to the designated area. However, my goal is for a single click on the button to loa ...

What is the best way to terminate a connection in @aspnet/signalr?

My project utilizes the dependency "@aspnet/signalr": "^1.1.4". Does anyone know how to properly close a connection in Angular's destructor? I attempted to use: this.connection.close(); ...

Is it possible to bind an http post response to a dropdown list in Angular?

My goal is to connect my post response with a dropdown list, but the text displayed in the drop-down box shows "[object Object]". My request returns two results - `ArticleID` and `Title`. I want to display `Title` in the dropdown and save the corresponding ...

What method can be used to specify a function of any signature that returns a particular type in programming?

I am looking to define a unique type that must be a function which, when executed, will always produce an object containing the property type: string. The input parameters for this function are of no concern. For instance: foo(1, 'bar'); // res ...

Encountering an "Unexpected token" error when importing a JSON file in TypeScript even though the JSON is valid

I recently read an article on Hacker Noon about importing JSON into TypeScript, and decided to give it a try in my code. Here's the import line I used: import data from './assets/fonts/helvetiker_bold.typeface.json'; To test if the import ...