简体   繁体   English

推入数组时的ngModel引用

[英]ngModel reference when pushed into array

I am pushing a new order into the customer.orders array and everything works fine but it seems to reference the ngModel because if I now type something into the neworder.product model it updates the view at customer.orders.product. 我正在将一个新订单推送到customer.orders数组中,一切正常,但它似乎引用了ngModel,因为如果我现在在neworder.product模型中键入内容,它会更新customer.orders.product中的视图。 It seems to reset itself on refresh. 它似乎在刷新时重置自己。

Any ideas? 有任何想法吗?

Some Code: 一些代码:

$scope.currentCustomer.orders.push($scope.newOrder);

The newOrder form: newOrder表单:

<form id="newOrder" ng-submit="submit()">
    <input type="month" ng-model="newOrder.date" />
    <div>
      <label ng-repeat="product in products">
        {{product}}
        <input type="number" ng-model="newOrder[product]" />
      </label>
    </div>
    <button>
        Add Order
    </button>
</form>

I display my orders: 我显示我的订单:

<div ng-repeat="order in currentCustomer.orders">
      <span class="header">
        {{order.date}}
      </span>
      <span ng-repeat="product in products">     
          {{product}}: {{order[products]}} 
      </span>
</div>

What happens: 1) Enter a new order and press enter. 会发生什么:1)输入新订单并按Enter键。 2) Enter a new order. 2)输入新订单。 3) All orders above get updated while typing? 3)以上所有订单在打字时都会更新? I don't understand because the ngModel is different? 我不明白因为ngModel不同?

I tried angular.copy 我试过angular.copy

You're using the same data object for all customer' orders. 您为所有客户订单使用相同的数据对象。 Use Angular' copy method, this way every order will have its own unique object: 使用Angular' copy方法,这样每个订单都有自己唯一的对象:

$scope.currentCustomer.orders.push( angular.copy($scope.newOrder) );

Working fiddle: http://jsfiddle.net/MZnML/ 工作小提琴: http//jsfiddle.net/MZnML/

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

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