缺少双向数据绑定和计算属性 - Riot.js

时间:2017-09-27 14:05:26

标签: riot.js riotjs

即使我知道我可以使用“event.target”添加一些事件来更新我的一些变量,但在更改某些分数时,如何更新平均值,如下例所示?

 <my-tag>
    <virtual each="{ criterias }">
        <label for="{ name }">{ name }:</label>
        <input type="number" min="0" max="10" value="{ score }" name="{ name }" id="{ name }">
    </virtual>
    <p>Average: { average }</p>

    <script>
        this.criterias = [
        {
            name: 'Plot',
            score: 8
        },
        {
            name: 'Audio',
            score: 9
        },
        {
            name: 'Graphics',
            score: 7
        }
        ]

        this.average = this.criterias.reduce((a, b) => a + b['score'], 0) / this.criterias.length
    </script>
</my-tag>

1 个答案:

答案 0 :(得分:0)

this.update()onclick之类的任何更改事件发生后调用

oninput。我相信您的平均值可以在更新函数内计算,如下所示:

this.on('update', function(){
    this.average = this.criterias.reduce((a, b) => a + b['score'], 0);
});
相关问题