简体   繁体   English

Angular.js:用户输入后更新过滤器

[英]Angularjs: update filter after user input

I am having an issue updating a value after user input using angularjs. 我在使用angularjs用户输入后更新值时遇到问题。 I am giving the user a grid to enter numbers into, and after each keypress, I would like to sum that column and display it in the column header. 我给用户一个网格,在其中输入数字,每次按键后,我想对该列求和并将其显示在列标题中。 The filter works for the initial value of table, but wont update when new number are input into the table. 该过滤器适用于表的初始值,但在将新数字输入到表中时不会更新。 Is there a way to refresh the element? 有没有一种刷新元素的方法?

<th>total<small>{{Rows|sumByKey:'ColOne'}}</small></th> ....

<tr ng-repeat="Row in Rows">
    <td><input type="text" value="{{Row.ColOne}}"/></td>
</tr>

Angularjs Filter: Angularjs过滤器:

app.filter('sumByKey', function () {
return function (data, key) {
    if (typeof (data) === 'undefined' || typeof (key) === 'undefined') {
        return 0;
    }

    var sum = 0;
    for (var i = data.length - 1; i >= 0; i--) {
        sum += parseInt(data[i][key]);
    }
    //filter.$stateful = true;
    return sum;
    };
});

I belive you need to bind you value to make angular aware of the changes. 我相信,您需要约束您的价值,以敏锐地意识到变化。 Use ng-model on the inputs to set the value. 在输入上使用ng-model来设置值。 This way Angular is in control of all values. 这样,Angular可以控制所有值。

<th>total<small>{{Rows|sumByKey:'ColOne'}}</small></th> ....

<tr ng-repeat="Row in Rows">
    <td><input type="text" ng-model="Row.ColOne"/></td>
</tr>

Here is a plunker for it running . 这是它运行的一个小工具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM