【问题标题】:How to update scope on form Submit, when ng-model value changes?当 ng-model 值更改时,如何更新表单提交的范围?
【发布时间】:2017-08-01 20:39:26
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    但以下会:

    Custom['foo'] = 'newValue';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多