【问题标题】:Input field blurs when inline editing in ng-repeat在 ng-repeat 中进行内联编辑时输入字段模糊
【发布时间】:2013-11-05 06:30:46
【问题描述】:

我正在尝试对数据表进行内联编辑(请参阅plunkr

<table class="table table-bordered">
<tr ng-repeat="data in dataset" >
  <td ng-repeat="(key, value) in data"  >

    <div class="key-block">
           <strong >{{key}}</strong>
        </div>
        <div class="val-block" inline-edit="data[key]" on-save="updateTodo(value)" on-cancel="cancelEdit(value)">
        <input type="text" on-enter="save()" on-esc="cancel()" ng-model="model" ng-show="editMode">
        <button ng-click="cancel()" ng-show="editMode">cancel</button>
        <button ng-click="save()" ng-show="editMode">save</button>

        <span ng-mouseenter="showEdit = true" ng-mouseleave="showEdit = false">
            <span ng-hide="editMode" ng-click="edit()">{{model}}</span>
            <a ng-show="showEdit" ng-click="edit()">edit</a>
        </span>
    </div>
  </td>
</tr>

我可以在很多地方看到,我们必须在 ng-model 中使用 . in ng-repeat 以避免范围问题。正如我dont know the key already 一样,我正在为模型做data[key]。 输入单个字符后,输入字段变得模糊。

【问题讨论】:

    标签: javascript json angularjs angularjs-ng-repeat


    【解决方案1】:

    您描述的行为是正常的。如果仔细观察,您会发现输入值和指令都绑定到同一个对象,即data[key]。当您更改文本输入的值时,model 得到更新,最终触发指令的刷新,您将返回“列表”视图。

    解决此问题的一个简单解决方案是在指令和输入值之间使用中间变量,并仅在单击保存按钮时更新模型。类似的东西:

      //Directive
      scope.newValue = null;
    
      scope.edit = function() {
        scope.editMode = true;
        scope.newValue = scope.model;
    
        $timeout(function() {
          elm.find('input')[0].focus();
        }, 0, false);
      };
    
      //Template
      <input type="text" on-enter="save()" on-esc="cancel()" ng-model="newValue" ng-show="editMode">
    

    你可以看到修改后的 plunker here

    【讨论】:

    • 感谢 Nicolas 的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多