我正在使用aurelia并想在 View 而不是 View 模型中过滤集合(数组)。

我正在尝试使用以下语法:

<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>

而且我在浏览器控制台中遇到以下错误:
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>

在 Aurelia 也可能有类似的事情吗?

我也很想知道如何在aurelia的repeat.for中过滤集合/数组(类似于ng-repeat)。我试图以类似的方式使用过滤器功能,但是它也无法正常工作,并且出现了类似的错误。

最佳答案

有点尴尬。但是经过一番研究(我应该事先做过: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>

我已经在TypeScript(valueconverters.ts)中定义了几个valueconverters:
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;
}
}

最后,我将这些导入了我的 View :<import from="yourPath/valueconverters"></import>

关于javascript - aurelia View 中的过滤器阵列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29162022/

10-09 23:07
查看更多