0

I'm new to Angular, I'm trying to set a value to an input using typescript to an HTML input, it is working fine however, when I submit the form it reads the value as an empty string, if I typed anything in the input in addition to the value that has been set the from submits with the value successfully, but doesn't submit if the value is set without changing anything manually, please advise if there is a better approach or how to correct this, thanks in advance.

company.component.html

<input type="text" id="LogoImageUrl" formControlName="LogoImageUrl">

company.component.ts

this.editCompanyForm = this.fb.group({
 LogoImageUrl: new FormControl({ value: ''})
})

this.image = (
  <HTMLInputElement>document.getElementById("LogoImageUrl")
).value = this.imageUrl


uploadFile(file) {
const contentType = file.type;
const bucket = new S3(
      {
          accessKeyId: 'key',
          secretAccessKey: 'key',
          region: 'region'
      }
  );
  const params = {
      Bucket: 'name',
      Key: 'url' + file.name,
      Body: file,
      ACL: 'public-read',
      ContentType: contentType
  };
  bucket.upload(params, function (err, data) {


      if (err) {
          console.log('There was an error uploading your file: ', err);
          return false;
      }
      console.log('Successfully uploaded file.', data);
      return true;
  }); 
  bucket.upload(params).on('httpUploadProgress', function (evt) {
      console.log(evt.loaded + ' of ' + evt.total + ' Bytes');
  }).send(function (err, data) {

      if (err) {
          console.log('There was an error uploading your file: ', err);
          return false;
      }
      this.imageUrl = data.Location;
      this.editCompanyForm.patchValue({
        'LogoImageUrl': this.imageUrl
      });  
      return true;
  });

  }
2
  • Can you create stackblitz? Commented Feb 6, 2020 at 12:30
  • I have updated my code, please check it out Commented Feb 6, 2020 at 13:29

3 Answers 3

1

You have to use arrow function for s3 instance to bind current component this context.

Try this:

component.ts

uploadFile(file) {
const contentType = file.type;
const bucket = new S3(
      {
          accessKeyId: 'key',
          secretAccessKey: 'key',
          region: 'region'
      }
  );
  const params = {
      Bucket: 'name',
      Key: 'url' + file.name,
      Body: file,
      ACL: 'public-read',
      ContentType: contentType
  };
  bucket.upload(params,(err, data)=> { 
      if (err) {
          console.log('There was an error uploading your file: ', err);
          return false;
      }
      console.log('Successfully uploaded file.', data);
      return true;
  }); 
  bucket.upload(params).on('httpUploadProgress', (evt)=>{
      console.log(evt.loaded + ' of ' + evt.total + ' Bytes');
  }).send(()=> (err, data) {

      if (err) {
          console.log('There was an error uploading your file: ', err);
          return false;
      }
      this.imageUrl = data.Location;
      this.editCompanyForm.patchValue({
        'LogoImageUrl': this.imageUrl
      });  
      return true;
  });

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

3 Comments

Are you getting same erro?
It finally worked, I can't thank you enough my friend
Glad to help my friend!
1

You can try

this.editCompanyForm.controls["LogoImageUrl"].setValue(this.imageUrl);

A form control instance provides a setValue() method that updates the value of the form control. You can look at https://angular.io/guide/reactive-forms

8 Comments

I've tried this and it is coming up with ERROR TypeError: Cannot read property 'controls' of undefined
and this ? this.editCompanyForm.get('LogoImageUrl').setValue(this.imageUrl);
unfortunately came up with the same "Cannot read property 'get' of undefined"
and this.editCompanyForm.patchValue({ LogoImageUrl: this.imageUrl }); ? your editCompanyForm is initialized? if it's still not ok, could you provide your complete code please
ERROR TypeError: Cannot read property 'patchValue' of undefined
|
0

Have you initialized the formbuilder in the ngOnInit ()? If you have not done so, it will give you undefined.

3 Comments

Yes, I have initialized it
try . ---> LogoImageUrl: [' '] and use this.editCompanyForm.controls.LogoImageUrl.setValue(this.imageUrl);
the problem presists in controls,,,it is coming up with undefined

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.