gpt4 book ai didi

javascript - 选定的 ng-option 与其数据不匹配以及空选项问题

转载 作者:行者123 更新时间:2023-12-03 10:25:44 27 4
gpt4 key购买 nike

我正在尝试在 AngularJS 中渲染范围变量的内容。这是我的代码。

<!doctype html>
<html ng-app="myApp">
<head>
<title> AngularJS Tabs</title>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

<style>

.box {
margin : 5px;
display : inline-block;
width: 150px;
height: 250px;
background-color: grey;
text-align:center;
vertical-align: top;
}

</style>
</head>

<body>

<div ng-controller="myCtrl">
<div class='box' ng-repeat="product in Products">
<br>
<b>{{product.BrandName}}</b>
<br>
{{product.ProductName}}
<br><br>
<img src="http://localhost/{{ product.ProductImagePath }}" alt="" border=3 height=75 width=75></img>
<br>
<br>

<select ng-init="SelectedVariant = product.Variants[0]" ng-model="SelectedVariant">
<option ng-selected="SelectedVariant"
ng-repeat="variant in product.Variants"
value="{{variant.VariantID}}">
{{variant.VariantName}}
</option>
</select>

<br>

<strike> {{SelectedVariant.MRP}} </strike> {{SelectedVariant.SellPrice}}

<br>
<button> Add to Cart </button>

</div>

</div>


<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.Products = [
{
"ProductID": "12",
"ProductName": "Green Detergent Bar",
"ProductImagePath": "/images/12.png",
"SubCategoryID": "1",
"SubCategoryName": "DetergentBar",
"BrandID": "1",
"BrandName": "Wheel",
"Variants": [
{
"VariantID": "1",
"VariantName": "500GM",
"VariantImagePath": "/images/12_1.png",
"MRP": "20.00",
"SellPrice": "19.50"
},
{
"VariantID": "2",
"VariantName": "1KG",
"VariantImagePath": "/images/12_2.png",
"MRP": "40.00",
"SellPrice": "38.00"
}
]
},
{
"ProductID": "13",
"ProductName": "Amla Hair Oil",
"ProductImagePath": "/images/13.png",
"SubCategoryID": "2",
"SubCategoryName": "HairOil",
"BrandID": "2",
"BrandName": "Dabur",
"Variants": [
{
"VariantID": "3",
"VariantName": "100ML",
"VariantImagePath": "/images/13_3.png",
"MRP": "30.00",
"SellPrice": "29.50"
},
{
"VariantID": "4",
"VariantName": "200ML",
"VariantImagePath": "/images/13_4.png",
"MRP": "60.00",
"SellPrice": "58.00"
}
]
}
];
});
</script>

</body>
</html>

上面的代码存在三个问题。

  1. 最初选择框显示第二个选项为选定状态,还有 1 个选项为空白条目。尽管一旦我选择列表中的任何选项,空白条目就会消失。但我希望默认选择选择框的第一个选项,并且页面加载时列表中没有空白选项。
  2. 当我在列表中选择新选项时,其价格没有变化。如何做到这一点?
  3. 加载时选择了第二个选项,但显示了第一个选项的价格。如何修复这个错误?

任何帮助将不胜感激。

最佳答案

使用ng-options将对象绑定(bind)到值。

<select ng-init="SelectedVariant = product.Variants[0]" ng-model="SelectedVariant" ng-options="variant.VariantName for variant in product.Variants"></select>

<强> DEMO

关于javascript - 选定的 ng-option 与其数据不匹配以及空选项问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29409904/

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