Say*_*Pal 13 javascript arrays angularjs mvw aurelia
我正在使用aurelia并希望在视图中而不是在视图模型中过滤集合(数组).
我正在尝试以下语法来执行此操作:
<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>
Run Code Online (Sandbox Code Playgroud)
我在浏览器控制台中遇到以下错误:
Error: Parser Error: Missing expected ) at column 28 in [errors.filter(function(err){return err.Key==='car.Model';]
.
这在angularJS中是可能的,如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
在aurelia也有类似的事情吗?
我也想知道如何repeat.for
在aurelia中过滤集合/数组(类似于ng-repeat
).我尝试以类似的方式使用过滤功能,但它也没有工作,我得到了类似的错误.
Say*_*Pal 27
这有点令人尴尬.但是经过一些研究(我应该事先做过:P)我得到了答案.
可以使用ValueConverter完成,如下所示:http://jdanyow.github.io/aurelia-converters-sample/ .
我认为这很酷.
现在我的代码看起来像这样:
<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>
Run Code Online (Sandbox Code Playgroud)
我在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;
}
}
Run Code Online (Sandbox Code Playgroud)
我终于在我看来导入了这些内容: <import from="yourPath/valueconverters"></import>