简体   繁体   中英

ng model inside ng-repeat of ng-repeat-start

I have following structure parent can have multiple children and children will have an objective. I need to show this in editable table, but when I bind the model within the input of objective, it updates all other selected children's objective too.

here is code

select same childrens from two row's options, and edit the objective of one children it will reflect in other row as well.

<tbody>
        <tr ng-repeat-start="parent in records" ng-class-even="'striped'">
            <!--KPI-->
            <td><strong>{{parent.name}}</strong></td>
            <!-- controls -->
            <td tool-tips class="inputs">
                <select
                        ng-model="parent.childrens"
                        ng-options="item as item.name for item in controlTypes"
                        multiple='multiple'>
                </select>
            </td>
            <!-- Controls objective-->
            <td colspan="1"/>
            </td>
        </tr>
        <tr ng-if="parent.childrens.length>0"
            ng-repeat="child in parent.childrens">
            <td name="process" colspan="2" style="word-wrap:break-all;" align="right">{{child.name}}
            </td>
            <!-- Controls objective-->
            <td>
                <input type="text" class="ultra-short" ng-model="child.objective"
                       maxlength="200"/>
            </td>

        </tr>
        <tr ng-repeat-end></tr>
        </tbody>

Any help please.

As far as I can tell it woun't work with such structure, because all you do is work with references. When you change some options objective property you actually change it everywhere in same option, because you are working with reference.

UPDATE:
One way to handle this

If you want the model to be different for each iteration of the ng-repeat:

<div ng-repeat="parent in parents">
  <select
    ng-model="vm.model.parent.item"
    ng-options="item as item.name for item in controlTypes"
    multiple="multiple">
  </select>
</div>

Now you have a different model for each iteration, is this what you were looking to do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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