簡體   English   中英

當ng-model值更改時,如何更新Form Submit的作用域?

[英]How to update scope on form Submit, when ng-model value changes?

我正在創建一個編輯客戶表單,並傳遞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> 

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

您正在使用引用該值的局部變量val 這基本上等於做

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

那不會改變Custom['foo'] ,對嗎?

但是以下內容將:

Custom['foo'] = 'newValue';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM