当 ng-model 值发生变化时,如何更新表单提交的范围?
How to update scope on form Submit, when ng-model value changes?
我正在创建一个编辑客户表单并传递一个包含 30 个键值对的对象。然后我使用 ng-repeat 来填充表单的输入字段。所有表格都显示得很好,并带有传递的键值。我还可以在提交表单时更改任何输入 field.But 的值,它采用我最初在表单中传递的旧范围对象,留下我的更改。我现在需要提交更改后的对象。怎么做?我搜索了很多但找不到解决方案。
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>
使用ng-model="Customer[key]"
。
您正在使用引用该值的局部变量 val
。这基本上等同于做
var val = Custom['foo'];
val = 'newValue';
这不会改变 Custom['foo']
的值,对吗?
但以下将:
Custom['foo'] = 'newValue';
我正在创建一个编辑客户表单并传递一个包含 30 个键值对的对象。然后我使用 ng-repeat 来填充表单的输入字段。所有表格都显示得很好,并带有传递的键值。我还可以在提交表单时更改任何输入 field.But 的值,它采用我最初在表单中传递的旧范围对象,留下我的更改。我现在需要提交更改后的对象。怎么做?我搜索了很多但找不到解决方案。
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>
使用ng-model="Customer[key]"
。
您正在使用引用该值的局部变量 val
。这基本上等同于做
var val = Custom['foo'];
val = 'newValue';
这不会改变 Custom['foo']
的值,对吗?
但以下将:
Custom['foo'] = 'newValue';