简体   繁体   中英

Aurelia, converter use inside view model

Using Aurelia, i'm trying to use a converter inside a view model. But I don't know how to do it or if it's even possible.

With AngularJS, for example:
inside a view

<span>{{ 'hello' | uppercase }}</span>

inside a controller

$filter('uppercase')('hello');

With Aurelia
inside a view

<span>${ 'hello' | uppercase }</span>

inside a viewmodel

?????????

In your viewmodel add the following code:

export class UppercaseValueConverter {
  toView(value) {
    return value?.toUpperCase();
  }
}

and then in your view:

<h1>${message | uppercase}</h1>

See a working example .

You can also add the value converter to the global resources section of your app to share it among all views.

Ok so, this is simple.
As our converters are classes, we juste have to call the method toView from an instance.

import { UppercaseValueConverter } from './converters';

const convertedValue = new UppercaseValueConverter().toView('Hello');

In my case I was a little bit lost, because I use a library and can't import the converter class directly.

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