gpt4 book ai didi

javascript - ng-model 显示重复值

转载 作者:行者123 更新时间:2023-12-03 11:38:18 25 4
gpt4 key购买 nike

以下是我的代码,请看一下

var app = angular.module('myApp', []);

app.controller('displayData', function($scope){
$scope.selected = {};

$scope.formData = {};
$scope.customproductoption = [
{
"id" : "1",
"producttype" : "Shoe",
"sizetype" : [
{ "size" : "15" },
{ "size" : "16" },
{ "size" : "17" }
]
},{
"id" : "2",
"producttype" : "Dress",
"sizetype" : [
{ "size" : "XS" },
{ "size" : "S" },
{ "size" : "L" }
]
}
];
});
<html ng-app="myApp">

<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<title></title>

</head>
<body ng-controller="displayData">
<select ng-model="selected.producttype" ng-options="s.producttype for s in customproductoption">
<option value="">-- Custom Product Option --</option>
</select>

<table class="table">
<thead>
<tr>
<th>Index</th>
<th>Size</th>
<th>Price</th>
<th>Qty</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="s in selected.producttype.sizetype">
<td>{{$index+1}}</td>
<td><input type="text" ng-model="formData.customsize" ng-init="formData.customsize=s.size" id="productsize" disabled></td>
<td><input type="text" ng-model="formData.customproductprice" id="customprice" name="productprice"></td>
<td><input type="text" ng-model="formData.customproductqty" id="customqty" name="productqty"></td>
</tr>
</tbody>
</table>
</body>
</html>

在此基于所选选项的代码中,我在相关结果中显示相关结果,用户可以填写产品价格和数量,在 ng-model 使用的 formData 中,我可以将所有填充的值绑定(bind)在一个数组中,但 ng-model 值显示所有重复值

最佳答案

var app = angular.module('myApp', []);

app.controller('displayData', function($scope){
$scope.selected = {};

$scope.formData = {};
$scope.customproductoption = [
{
"id" : "1",
"producttype" : "Shoe",
"sizetype" : [
{ "size" : "15" },
{ "size" : "16" },
{ "size" : "17" }
]
},{
"id" : "2",
"producttype" : "Dress",
"sizetype" : [
{ "size" : "XS" },
{ "size" : "S" },
{ "size" : "L" }
]
}
];
});
<html ng-app="myApp">

<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<title></title>

</head>
<body ng-controller="displayData">
<select ng-model="selected.producttype" ng-options="s.producttype for s in customproductoption">
<option value="">-- Custom Product Option --</option>
</select>

<table class="table">
<thead>
<tr>
<th>Index</th>
<th>Size</th>
<th>Price</th>
<th>Qty</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="s in selected.producttype.sizetype">
<td>{{$index+1}}</td>
<td><input type="text" ng-model="formData.customsize[$index]" ng-init="formData.customsize[$index]=s.size" id="productsize" disabled></td>
<td><input type="text" ng-model="formData.customproductprice" id="customprice" name="productprice"></td>
<td><input type="text" ng-model="formData.customproductqty" id="customqty" name="productqty"></td>
</tr>
</tbody>
</table>
</body>
</html>

重复时您将替换 formData.customsize 值。所以你必须分配不同的模型

关于javascript - ng-model 显示重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26381198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com