0

我正在创建一个编辑客户表单并传递一个包含 30 个键值对的对象。然后我使用 ng-repeat 填充表单的输入字段。所有表单都很好地显示并带有传递的键值。我还可以更改任何输入字段的值。但是当我提交表单时,它会采用我最初在表单中传递的旧范围对象,而留下我的更改。我现在需要提交我更改的对象。怎么做?我搜索了很多,但找不到解决方案。

  var app = angular.module("customerModule", []);
app.controller('crudController', function($scope, $http) {
  $scope.Customers = //object from Ap with almost 30 key,values

    $scope.edit = function() {
      console.log($scope.Customers);
      //the above line prints the API called object where as I am editing the values of ng-model in my form. and I need the form submitted values
    }


});
<div ng-app="customerModule" ng-controller="crudController">
  <form name="as" ng-submit="edit()">
    <ul>

      <li ng-repeat="(key, val) in Customers  " ng-hide="(key=='total' || key=='paid' || key=='customfields' || key=='owing')" ng-if="key!='customfields'">

        <label class="label"> {{key}}</label> <input type="text" ng-model="val" />
      </li>

      <li ng-repeat="(key, val) in Customers.customfields">
        <label class="label"> {{key}}</label> <input type="text" ng-model="val" />

      </li>
      <button type="submit"><i class="fa fa-plus-circle" aria-hidden="true"></i><span> Edit Customer</span></button>
      <ul>

  </form>

</div>

4

1 回答 1

1

使用ng-model="Customer[key]".

您正在使用val引用该值的局部变量。这基本上相当于做

var val = Custom['foo'];
val = 'newValue';

这不会改变 的值Custom['foo'],对吧?

但以下将:

Custom['foo'] = 'newValue';
于 2017-03-11T14:55:46.643 回答