从Angular JS中的ng-repeat输入设置ng-model值?

时间:2019-04-15 11:23:49

标签: angularjs angularjs-ng-repeat angularjs-ng-model

我正在尝试允许客户使用ngRepeat和ngModel编辑项目列表。但是我无法设置对输入字段的响应 这是代码。

<form name="customerupdateForm" ng-submit="EditCustomerSubmit(updatecustomer)" class="horizontal-form">                             
    <div class="row-fluid" ng-repeat="updatecustomer in itemList">
        <div class="control-group">
        <label class="control-label">Customer Name</label>
        <input type="text" name="customer_name" ng-model="updatecustomer.customer_name" class="span6 m-wrap"/>                                          
        </div>
        <!--/span-->
        <div class="control-group">
        <label class="control-label">Company Name</label>
        <input type="text" name="company_name" ng-model="updatecustomer.company_name" class="span6 m-wrap"/>                                            
        </div>
    </div>
</form>

Angular.js

$scope.id = $routeParams.id;

$scope.getupdateCustomerdata = function(id){
    $http({
        method: 'get',
        url: 'api/master/customer/getupdatecustomer?id=' + $routeParams.id
    }).then(function successCallback(response) {

        console.log(response.data['customer_name']);
        $scope.updatecustomer = {};
        //set response to input filed
        $scope.updatecustomer.customer_name = response.data['customer_name'];
    }); 
}
$scope.getupdateCustomerdata();

1 个答案:

答案 0 :(得分:1)

我假设您的api返回了Customers列表,在这种情况下,您必须使angularjs中的api响应等于itemList

.then(function successCallback(response) {

    console.log(response.data['customer_name']);
    $scope.updatecustomer = {};
    //set response to input filed
    $scope.itemList = response;
});

如果您的响应是对象列表,并且具有名为customer_namecompany_name的属性,那么您的输入元素将填充模型值