Search code examples
javascriptangulartypescriptionic-frameworkcapacitor

Angular / Typescript using the parameter value from method as a js object value


I am using Ionic / Angular / Typescript and I want to dynamically state the value of a location by the value passed from the method in the parameter.

Here is the code:

async fileWrite(location) {
    try {
      const result = await Filesystem.writeFile({
        path: `test.txt`,
        data: `This is a test`,
        directory: location,
        encoding: FilesystemEncoding.UTF8
      });
      alert('Wrote file' + result);
    } catch (e) {
      alert('Unable to write file' + e);
    }
  }

As you can see:

directory: location,

location contains a value so it's a variable.

I'm passing it like this:

<ion-button (click)="fileWrite('FilesystemDirectory.Cache')">FileWrite to Cache</ion-button>

For some reason it's not passing as a variable.

How can I fix this?


Solution

  • Try doing this.

    In your typescript class, right before constructor.

    directory = FilesystemDirectory;
    

    and then inside you template.

    <ion-button (click)="fileWrite(directory.Cache)">FileWrite to Cache</ion-button>
    

    Hope this helps.