javascript - 使用 native 脚本转换器

标签 javascript android nativescript

我现在正在尝试使用 JavaScript 来连接 NativeScript,并有一个非常基本的问题:

    let costFormatter= {
        toView(value){
            console.log('Got:' + value);
            return '$' + value;
        },
        toModel(value){
            console.log('Got:' + value);
            return '$' + value;
        }
    };
    http.getJSON("<My API Call>").then(function (r){
        page.bindingContext = {
            deals: r,
            costFormatter:costFormatter
        };
    }, function (e){
        //// Argument (e) is Error!
        //console.log(e);
    });

在上面的代码中,我定义了成本格式化程序,我只想在 ListView 标签上每个销售价格的价格旁边添加一个 $。要渲染 ListView ,我使用:

<ListView id="SteamItems" items="{{ deals }}">
                        <ListView.itemTemplate>
                            <GridLayout columns="*, *, 50, 50" rows="50, 50, auto, *">
                                <Image src="{{ thumb }}" row="0" col="0" cssClass="thumb"/>
                                <Label text="{{ title }}" key="1" row="0" col="1" cssClass="title"/>
                                <Label text="{{ normalPrice|costFormatter }}" key="2" row="0" col="2" cssClass="normal-price"/>
                                <Label text="{{ salePrice|costFormatter }}" key="3" row="0" col="3" cssClass="sale-price"/>

                            </GridLayout>

                        </ListView.itemTemplate>
                    </ListView>

我不明白为什么我会得到

JS: Cannot find function or filter: costFormatter

对于 native 脚本终端中 ListView 中的每一行。我做错了什么?

最佳答案

您似乎正在尝试创建一个所谓的“管道”。

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'costFormatter',
  pure: true
})

export class CostFormatterPipe implements PipeTransform {
  transform(price: string): string {
    return "$ " + price;
  }
}

然后确保将 CostFormatterPipe 添加到要使用它的模块的 Declarations 数组中。

关于javascript - 使用 native 脚本转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43270138/

相关文章:

javascript - 如何 chop 字符串并删除尾随字符(如果存在)?

javascript - D3.js 决策树 - 文本换行、边界框、负载圈数

android - 未找到以下类 : android. support.v4.widget.DrawerLayout

android - 消费者关闭输入 channel 或发生错误。事件=0x8

android - 您使用 NativeScript 的体验如何?

javascript - 如何映射对象数组并更改样式?

javascript - foreach 中的 Discord.JS addField 不起作用

android - 从 GCM onMessage() 调用 MainActivity 内的方法

android - 图片前面的 Nativescript CSS 文本

firebase - 我无法获取要使用 nativescript 上传的文件的大小