angularjs - Angular Js orderBy with input values
问题描述
I'm having issues with the orderBy filter. The following code will order my initiative column just fine. When I type a value into the input, the filter automatically begins ordering the values just as I want it to.
However, if I type a value into the input that raises that character higher in the list the input will close out before I can finish typing that value.
If the value I type into the input drops the character lower down the list then that input does not close and allows me to finish typing my value.
Can anyone explain this behavior?
<tr ng-repeat="char in localChars | orderBy: '-initiative'">
<td>{{char.name}}</td>
<td ng-hide='show' ng-click='show = true'>{{char.initiative}}</td>
<td ng-show='show'>
<input ng-blur='initiative(char)' ng-model='char.initiative' type="text">
</td>
</tr>
解决方案
以下是我认为的事件顺序:
- 用户在输入中输入更改 的值
char.initiative
。 - 这会导致列表通过
ng-repeat
. - 的作用域
ng-repeat
被重新初始化,导致局部show
变量失去它的值(记住,ng-repeat
有它自己的作用域)。
要解决此问题,您需要使用ng-model-options
来控制模型值实际更新的时间。您可以选择更新模糊或使用去抖动。这是更新模糊的方法(我的建议)。您可以在链接中找到有关去抖动的信息。
<input ng-blur="initiative(char)"
ng-model="char.initiative"
ng-model-options="{ updateOn: 'blur' }"
type="text" />
推荐阅读
- laravel - Laravel 中的错误屏幕
- javascript - 通过相同属性的值按其他对象过滤对象数组
- javascript - 具有从右到左水平滚动的每月时间轴网格
- sql-server-2008 - 在 SQL 中显示过去 3 年每个月的摘要数据
- c# - if else if 对于变量 C# 的多个可能值
- reactjs - React Native 中的 Jest 和 React 导航出错
- sql - 将字符串的所有实例提取到连接结果中
- asp.net-mvc - 验证用户登录后无法调用下一个操作。错误将您重定向太多次
- javascript - 为什么 Axios .then 和 .catch 函数会增加我的索引?
- android - setDisplayHomeAsUpEnabled(true) 打开选项菜单