我现在正在尝试使用 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/