当ng-model值发生变化时,如何更新表单提交范围?

时间:2017-03-11 14:28:02

标签: angularjs forms angularjs-ng-repeat ng-bind ng-submit

我正在创建一个编辑客户表单并传递一个包含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>

1 个答案:

答案 0 :(得分:1)

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

您正在使用引用该值的本地变量val。这基本上是和谐的。

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

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

但以下是:

Custom['foo'] = 'newValue';