【问题标题】:ng-repeat execution forces input to lose focusng-repeat 执行强制输入失去焦点
【发布时间】:2014-09-04 01:38:02
【问题描述】:

我有一个使用 ng-repeat 创建的输入

    <div data-ng-repeat="(index, answer) in currentQuestion['possible_answers']" class="form-group">
        <label class="col-md-3 control-label">Answer {{ index + 1 }}</label>
        <div class="col-md-8">
            <div class="input-icon">
                <i class="fa fa-sun-o"></i>
                <input data-ng-model="currentQuestion['possible_answers'][index]" type="text" class="form-control" >
            </div>
        </div>
    </div>

我希望它使用 currentQuestion['possible_answers'] 中的值预填充输入,并且我还希望任何更改都绑定到此变量。

但是,每次我开始在其中一个文本字段中输入内容时,我都会输入一个字母,然后它就会失去输入框的焦点。我有一种感觉,这是因为我开始打字并且数据竞价更新currentQuestion。因为currentQuestion更新了,ng-repeat又被执行了。

有没有办法让ng-repeat 操作成为一次性操作,而不是不断重新评估?

【问题讨论】:

  • 您可以尝试绑定到answer,而不是使用索引并再次在您的数组中查找它吗?

标签: angularjs input angularjs-ng-repeat


【解决方案1】:

是的 (looking at the symptoms, you did not show us the data) 你的问题可能是因为你的模型是你(可能有)数组中的文本,所以每当你更新模型时,它都会触发摘要循环,因为 ng -repeat 由文本跟踪。您可以通过提供轻松解决此问题。 track by $index,这样 ng-repeat 就会被监视,并且只有在数组长度发生变化时才会更新重复监视。

 <div data-ng-repeat="answer in currentQuestion['possible_answers'] track by $index" class="form-group">
    <label class="col-md-3 control-label">Answer {{ $index + 1 }}</label>
    <div class="col-md-8">
        <div class="input-icon">
            <i class="fa fa-sun-o"></i>
            <input data-ng-model="currentQuestion['possible_answers'][$index]" type="text" class="form-control" >
        </div>
    </div>
</div>

Demo

您也可以使用$index 来获取数组的索引。您不需要使用 (key, value) 进行迭代。

但是我只是将我的答案数组变成一个对象数组并摆脱所有这些问题,它只是(_注意$indexng-model的用法):-

<div data-ng-repeat="answer in currentQuestion['possible_answers'] track by $index" class="form-group">
    <label class="col-md-3 control-label">Answer {{ $index + 1 }}</label>
    <div class="col-md-8">
        <div class="input-icon">
            <i class="fa fa-sun-o"></i>
            <input data-ng-model="answer.text" type="text" class="form-control" >
        </div>
    </div>
</div>

Demo

【讨论】:

  • track by $index 是我想要的。仅当项目数发生变化时才执行 ng-repeat。
  • @moesef 是的。当您处理对象时,您也可以使用对象上的唯一属性来设置跟踪。基本上发生的情况是,当您使用 ng-repeat 跟踪时,除非需要,否则不会删除和重新创建 DOM,如果底层对象发生更改,那么它只会更新。如果您处理对象并且您没有指定 track by 它将向它们添加一个唯一键 $$hashKey ,因此如果您刷新列表它将再次重新创建它们,如果您刷新它会使用 track by 它只会更新现有元素并只删除那些需要的。
  • 这是非常棒的功能。感谢您的帮助。
【解决方案2】:

ng-repeat 为列表中的每个项目创建一个新的子范围。在这个范围内,它知道indexanswer。您将输入的值绑定到范围之外的内容,即数组中的相同项目。更改它会触发重绘列表,从而导致输入失去焦点。

<div data-ng-repeat="(index, answer) in currentQuestion['possible_answers']" class="form-group">
  <label class="col-md-3 control-label">Answer {{ index + 1 }}</label>
  <div class="col-md-8">
   <div class="input-icon">
      <i class="fa fa-sun-o"></i>
      <input data-ng-model="answer" type="text" class="form-control" >
    </div>
  </div>
</div>

【讨论】:

  • 试过了,但是 currentQuestion 不能正确绑定
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 2021-12-14
相关资源
最近更新 更多