2

I am having the below code part.

    ngOnInit(): void {
        this.form = this.formBuilder.group({
            apiCodesFile: ['']
        });
    }

    public onFileSelect(event) {
        if (event.target.files.length > 0) {
            this.isFileSelected = true;
            const file = event.target.files[0];
            this.form.get('apiCodesFile').setValue(file);
        } else {
            this.isFileSelected = false;
        }
    }

But I need to use get/set methods to set the form value for the formControl 'apiCodesFile' rather than setting it inside 'onFileSelect' method.

I added the below code parts. But it gave an error.

    set apiCodesFile (val) {
        this.form.get('apiCodesFile').setValue(val);
    }

    public onFileSelect(event) {
        if (event.target.files.length > 0) {
            this.isFileSelected = true;
            const file = event.target.files[0];
            this.apiCodesFile(file);
        } else {
            this.isFileSelected = false;
        }
    }

Error:

ERROR TypeError: this.apiCodesFile is not a function

Please give me a solution for this.

4
  • Does this answer your question? Accessors: setter does not work Commented Oct 4, 2021 at 5:52
  • No that way did not work at all Commented Oct 4, 2021 at 6:02
  • If you use setter, you need to apply this.apiCodesFile = "value" to set value. Commented Oct 4, 2021 at 6:16
  • Use patchValue to set the value. Check this Patch_Value_Example Commented Oct 4, 2021 at 19:00

1 Answer 1

2

Please try using below code

apiCodesFile (val) {
   this.form.get('apiCodesFile').setValue(val);
}

Remove the set from apiCodesFile Function

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.