简体   繁体   English

Aurelia,转换器使用内视图 model

[英]Aurelia, converter use inside view model

Using Aurelia, i'm trying to use a converter inside a view model.使用 Aurelia,我正在尝试在 model 视图中使用转换器。 But I don't know how to do it or if it's even possible.但我不知道该怎么做,或者是否有可能。

With AngularJS, for example:以 AngularJS 为例:
inside a view在视图内

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

inside a controller 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.由于我们的转换器是类,我们只需要从实例中调用 toView 方法。

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.就我而言,我有点迷茫,因为我使用了一个库并且无法直接导入转换器 class 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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