问题描述
我使用并希望过滤鉴于集合(阵列),而不是在视图模型。
I am using aurelia and want to filter a collection (array) in view rather than in view model.
我想下面的语法这样做的:
I am trying the following syntax to do so:
<div class="col-sm-7 col-md-7 col-lg-7 ${errors.filter(function(err){return err.Key==='car.Model';}).length >0? 'alert alert-danger' :''}">
<span repeat.for="error of errors">
<span if.bind="error.Key==='car.Model'">
${error.Message}
</span>
</span>
</div>
和我正在下面的浏览器控制台错误:
And I am getting following error in browser console:
错误:分析器错误:在第28栏缺少预期)errors.filter(功能(错误){返回err.Key ==='car.Model']
。
这是可能的angularJS如下:
This is possible in angularJS as follows:
<div class="small-7 medium-7 large-7 columns end">
<span class="error" ng-repeat="error in errors | filter:{ Key: 'car.Model'}">
<span class="error-message">
{{error.Message}}
</span>
</span>
</div>
在Aurelia酒店类似的事情也可能吗?
Is similar thing also possible in aurelia?
我也很想知道如何收集/阵列可以在 repeat.for
过滤在奥里利亚(类似于 NG-重复
)。我试图使用过滤功能以类似的方式,但它也没有工作,我得到了类似的错误。
I would also love to know how a collection/array can be filtered in repeat.for
in aurelia (similar to ng-repeat
). I tried to use filter function in similar fashion but it too didn't work and I got similar error.
推荐答案
这是一个有点尴尬。但一个小小的研究(这是我应该做的事前:P)后,我已经得到了答案。
It's a little embarrassing. But after a little research (which I should have done beforehand :P) I have got the answer.
可以使用ValueConverter做如下所示:http://jdanyow.github.io/aurelia-converters-sample/.
It can be done using ValueConverter as shown here: http://jdanyow.github.io/aurelia-converters-sample/.
我认为这是相当凉爽。
现在我的code是这样的:
Now my code looks like this:
<div class="col-sm-7 col-md-7 col-lg-7 alert alert-danger" if.bind="errors | hasPropertyValue:'Key':'car.Model'">
<span repeat.for="error of errors | filterOnProperty:'Key':'car.Model'">
<span>
${error.Message}
</span>
</span>
</div>
和我在打字稿定义夫妇valueconverters的( valueconverters.ts
)
And I have defined couple of valueconverters in TypeScript (valueconverters.ts
):
export class FilterOnPropertyValueConverter {
toView(array: {}[], property: string, exp: string) {
if (array === undefined || array === null || property === undefined || exp === undefined) {
return array;
}
return array.filter((item) => item[property].toLowerCase().indexOf(exp.toLowerCase()) > -1);
}
}
export class HasPropertyValueValueConverter {
toView(array: {}[], property: string, exp: string) {
if (array === undefined || array === null || property === undefined || exp === undefined) {
return false;
}
return array.filter((item) => item[property].toLowerCase().indexOf(exp.toLowerCase()) > -1).length > 0;
}
}
和我最后的进口,这些在我看来:&LT;进口来自=yourPath / valueconverters&GT;&LT; /进口&GT;
And I finally imported these in my view: <import from="yourPath/valueconverters"></import>
这篇关于器阵列奥里利亚视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!