简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM