如何对来自repeat.for循环的函数参数进行值绑定?

问题描述 投票:0回答:1

我需要从Aurelia repeat.for html模板的下拉选项中预先选择一个值。我只是不知道如何将变量field传递给get getValue(field)函数。我想从repeat.for =“字段的字段”数据中传递它。也许由于某些绑定行为,这是不可能的,或者我需要使用一些特殊的语法,例如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

@computedFrom('fragmentedData')
get getValue(field) {
    // if field is available in fragmentedData, return value, else 0 for empty option
}
drop-down-menu parameters bind aurelia repeat
1个回答
0
投票

我用数组容器解决了问题,在该容器中存储了连接。在view.js的激活函数中,初始化一个数组,如:

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

现在我们可以在view.html中使用value.bind了($ index是repeat.for中的自动值,由repeat.for =“ fields of field”确定:]

<select class="form-control" value.bind="storedConnections[$index]">

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

© www.soinside.com 2019 - 2024. All rights reserved.