Aurelia获得价值召集人的结果

Ale*_*nko 10 javascript aurelia

我想获得value conventer的结果,在我的视图中过滤一个数组,以显示找到的结果数.

  <div repeat.for="d of documents|docfilter:query:categories">
    <doc-template d.bind="d"></doc-template>
  </div>
Run Code Online (Sandbox Code Playgroud)

我既不想将这个逻辑移动到我的控制器(保持干净),也不想添加拐点,比如从值控制器返回一些数据.

我想要的是:

所以,基本上我想是这样的角度报价:像显示在这里: ng-repeat="item in filteredItems = (items | filter:keyword)" 或这里:ng-repeat="item in items | filter:keyword as filteredItems"

我得到了什么:

不幸的是,在Aurelia:

d of filteredDocuments = documents|docfilter:query:categories

实际上意味着d of filteredDocuments =文件 |docfilter:query:categories,如果我添加括号as,它将无法运行(失败并出现解析器错误).

所以,

是否有一种干净的方式从数据过滤器中获取数据?

此致,亚历山大


UPD 1:当我谈到从值控制器返回一些数据时,我的意思是:

export class DocfilterValueConverter {
  toView(docs, query, categories, objectToPassCount) {
    ...
    objectToPassCount.count = result.length;
    ...
  });
});
Run Code Online (Sandbox Code Playgroud)

UPD 2.实际上,我错了:d of filteredDocuments = documents |docfilter:query:categories.它没有解决问题,但这段代码的作用是:

1)filteredDocuments = documents |docfilter:query:categories在init 2)上d of filteredDocuments,它是在最开始的数组上重复过滤的

Jer*_*yow 9

假设你有一个外部元素,你可以将过滤后的项目填充到ad-hoc属性中,如下所示:

<!-- assign the filtered items to the div's "items" property: -->
<div ref="myDiv" items.bind="documents | docfilter : query : categories">

  <!-- use the filtered items:  -->
  <div repeat.for="d of myDiv.items">
    <doc-template d.bind="d"></doc-template>
  </div>

</div>
Run Code Online (Sandbox Code Playgroud)

我知道这不是你想要的,但它会完成这项工作.我正在研究添加let绑定命令是否有帮助 - 如下所示:<div let.foo="some binding expression">

编辑

这里的东西更好一点:https://gist.run/ ? id = 1847b233d0bfa14e0c6c4df1d7952597

<template>
  <ul with.bind="myArray | filter">
    <li repeat.for="item of $this">${item}</li>
  </ul>
</template>
Run Code Online (Sandbox Code Playgroud)

  • 使用你可能更喜欢的`with`绑定添加了一个选项 (2认同)