Drop down menu 如何从repeat.for循环中对函数参数进行value.bind?

Drop down menu 如何从repeat.for循环中对函数参数进行value.bind?,drop-down-menu,parameters,bind,aurelia,repeat,Drop Down Menu,Parameters,Bind,Aurelia,Repeat,我需要从Aurelia repeat.for html模板中的下拉选项中预先选择一个值。我就是不知道如何将变量字段传递到get getValue(field)函数中。我想从repeat.for=“field of fields”数据中传递它。可能由于某些绑定行为,这是不可能的,或者可能我需要使用一些特殊语法,例如value.bind=“getValue($field)”。我很感谢你的帮助,或者给我一个可以搜索的提示。我尝试了一些aurelia文档和搜索机器搜索,但没有成功。或者暗示这个问题问得不

我需要从Aurelia repeat.for html模板中的下拉选项中预先选择一个值。我就是不知道如何将变量字段传递到get getValue(field)函数中。我想从repeat.for=“field of fields”数据中传递它。可能由于某些绑定行为,这是不可能的,或者可能我需要使用一些特殊语法,例如value.bind=“getValue($field)”。我很感谢你的帮助,或者给我一个可以搜索的提示。我尝试了一些aurelia文档和搜索机器搜索,但没有成功。或者暗示这个问题问得不恰当

view.html

<div repeat.for="field of fields">
    <select class="form-control" value.bind="getValue(field)">
        <option value="0">-</option>
        <option repeat.for="connection of connections" model.bind="connection.field.value">...</option>
    </select>
</div>

我用存储连接的数组容器解决了这个问题。在view.js的activate函数中,初始化一个数组,如下所示:

async activate(config)
    this.fields = // request for all fields
    this.fragmentedData = // request for fragmentedDate, which are an overlap of fields
    this.storedConnections = []
    for (let index = 0; index < this.fragmentedData.length; index++) {
        this.storedConnections[index] = this.getConnectionValue(
            this.fragmentedData[index]
        );
    }
}

getConnectionValue(fragmentedDataEntry) {
    for (let index = 0; index < this.fields.length; index++) {
        // check equality and return value if given
    }
    return 0;
}
异步激活(配置) this.fields=//请求所有字段 this.fragmentedData=//请求fragmentedDate,这是字段的重叠 this.storedConnections=[] for(让index=0;index 现在我们可以在view.html中使用value.bind,比如($index是repeat.for中的自动值,由repeat.for=“field of fields”)确定:



尽管如此,如果有一种语法可以像原始问题中那样传递参数,那可能会很有趣。这个技巧在某种程度上是一个解决办法,但它确实起到了作用。它只需要对更改事件做出反应,以存储和更新缓存的数组。

在上面的代码中,实际传递给getValue的是什么?碎片数据[字段]中有什么?为什么@computedFrom?由
getValue
返回的值是字符串、数字还是对象?@Benny:字段当前是一个对象,我只需要id进行相等性检查。我也可以像getValue(field.id)一样传递field.id。fragmentedData[索引]仅存储字段中的重叠项。例如,字段有许多ID为[1…15]的条目,fragmentedData有所有ID为[4,7,11,12]的可用字段。用户可以使用下拉列表配置进一步的连接,并可以向fragmentedData添加或删除ID。这就是此连接存储视图的主要原因。@Ashley Grant:当前一个对象具有一些属性,例如id(数字)、名称(字符串)。我可以将其更改为只传递所需的id,因此我们可以将其重构为getValue(field.id)。与其创建一个新的数组
storedConnections
,不如为每个
字段添加一个属性
?然后-您不需要使用
storedConnections[$index]
,而是可以使用类似于
field.connection的内容
async activate(config)
    this.fields = // request for all fields
    this.fragmentedData = // request for fragmentedDate, which are an overlap of fields
    this.storedConnections = []
    for (let index = 0; index < this.fragmentedData.length; index++) {
        this.storedConnections[index] = this.getConnectionValue(
            this.fragmentedData[index]
        );
    }
}

getConnectionValue(fragmentedDataEntry) {
    for (let index = 0; index < this.fields.length; index++) {
        // check equality and return value if given
    }
    return 0;
}
<select class="form-control" value.bind="storedConnections[$index]">