简体   繁体   English

带socket.io的AngularJS ng-repeat嵌套表格

[英]AngularJS ng-repeat nested form w/ socket.io

I am building a real time application with socket.io. 我正在用socket.io构建一个实时应用程序。 I've ran across a problem using ng-repeat with a nested form inside an ng-repeat tag. 我在使用ng-repeat标记内嵌套形式的ng-repeat时遇到了一个问题。 Here is the jist of the code: 这是代码的原理:

<div class="row" id="thingArea">
        <div class="thing text-center col-sm-{{12/bigObject.columns.length}} col-xs-{{12/bigObject.columns.length}}" ng-repeat="column in bigObject.columns" data-columnindex="{{$index}}" id="column{{$index}}">
          <h3 class="title">
            <span class="text header-text"><font size="7" ng-bind-html="column.title"></font> </span>
          </h3>
          <form ng-submit="addRow($index, rowValue)" class="thing-form" >
            <div class="form-group">
              <input type="text" class="form-control" ng-model="rowValue" placeholder="{{column.placeholder}}">
              <br/>
              <br/>
              <div class="thing-column" as-sortable="thingSortOptions" ng-model="column.rows">
                <div class="alert alert-{{column.color}} alert-dismissible" role="alert" ng-model='row' ng-repeat="row in column.rows" as-sortable-item>
                  <div as-sortable-item-handle>
                    <button type="button" class="close" ng-click="deleteRow(column, row)" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    {{row.value}}
                  </div>
                </div>
              </div>
            </div>
          </form>
        </div>
      </div><!--//row-->

The schema of bigObject is as follows: bigObject的架构如下:

var RowSchema = new Schema({
  value: String
});

var ColumnSchema = new Schema({
  title:  String,
  placeholder: String,
  color: String,
  selected: String,
  rows: [RowSchema]
});

var BigObjectSchema = new Schema({
  _id: {
    type: String,
    unique: true,
    'default': shortid.generate
  },
  name: String,
  startTime: Date,
  endTime: Date,
  columns: [ColumnSchema],
  info: String,
  active: Boolean
});

Basically bigObject can have N columns, with N rows inside of it. 基本上,bigObject可以有N列,其中有N行。 This is where I'm using ng-repeat to first iterate over the columns, which contains a form with an input to add rows. 在这里,我使用ng-repeat首先遍历各列,该列包含一个带有输入以添加行的表单。 Then within the column I then iterate over the rows. 然后在列中,然后遍历各行。

Okay. 好的。 Here's the my problem. 这是我的问题。 Since I'm using socket.io to sync real time bigObject updates between clients when the bigObject changes it re renders ALL of the ng-repeat's...and in dosing so blows away the input . 由于我使用socket.io来在bigObject更改时在客户端之间实时同步bigObject更新,因此它将重新呈现所有ng-repeat的...,并且在给药时会浪费输入 As in, if one client is typing in an input, and another client causes an update, the user receiving the update page re renders and whatever they are type gets blown away. 就像在其中,如果一个客户端正在输入输入,而另一客户端导致了更新,则接收到更新页面的用户将重新呈现,并且无论键入什么内容都会被吹走。

My thoughts so far: 到目前为止,我的想法是:

  • Take the input box out of all the ng-repeat tags, so its unaffected from updates. 从所有ng-repeat标签中取出输入框,使其不受更新的影响。 This adds complexity however, again I could have N columns...means N input boxes. 这增加了复杂性,但是我又可以有N列...意味着N个输入框。 It's nice having them correlate. 让它们相互关联真是太好了。
  • Ditch the idea of being completely dynamic in terms of N columns since in reality the UI with be fixed to only allow 2-3 columns. 放弃了关于N列完全动态的想法,因为实际上UI被固定为仅允许2-3列。 But ya, that seems lame. 但是,这似乎很la脚。
  • Find some fancy angular/css tricks to hack around this....which haven't been super fruitful 找到一些花哨的角度/ CSS技巧来解决这个问题。

Am I approaching this all wrong? 我是不是都错了? I usually favor the simplest option..the first option adds code complexity, and the second adds code duplication but more simple. 我通常喜欢最简单的选项。.第一个选项增加代码复杂度,第二个选项增加代码重复,但是更简单。

Thanks. 谢谢。

You could change it like this: 您可以这样更改:

<div....ng-repeat="column in bigObject.columns" ng-init="columnIndex = $index"....>

Replace ng-model="rowValue" to something like ng-model="temp[columnIndex].rowValue" , where temp is another object in your model, unrelated to BigObject. ng-model="rowValue"替换为ng-model="temp[columnIndex].rowValue" ,其中temp是模型中的另一个对象,与BigObject不相关。 This way your temporary (not yet saved data, and available to only the current client) is in a different place than the one available to all clients, and the update of BigObject shouldn't interfere with the text in your input. 这样,您的临时文件(尚未保存的数据,仅对当前客户端可用)与所有客户端可用的文件夹位于不同的位置,并且BigObject的更新不应干扰输入中的文本。

Edit: Not sure if I was clear enough(or if it's the recommended way to do it), but temp should be on the model, not only on the scope(since the scope gets replaced and it loses the content) 编辑:不确定我是否足够清楚(或者是否建议这样做),但是温度应该在模型上,不仅在范围上(因为范围被替换并且丢失了内容)

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

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