[英]Ng-init from an ng-repeat
我有一个产品列表,您可以看到JSON:
var panier =
{
"Client": "Ben",
"Date":"",
"Produits": [
{
"Id": "188",
"Name":"Name1",
"Qte": "5",
"Variante":
"20 Pcs",
"Prix" :"149.00"
},
{
"Id": "231",
"Name":"Name2",
"Qte": "2",
"Variante": "7 encas de 35g",
"Prix" :"109.00"
},
{
"Id": "232",
"Name":"Name3",
"Qte": "7",
"Variante": "10 pieces",
"Prix" :"99.00"
}
]
};
我用ionic列出了要检索的产品,所有这些都可以使用。
当我执行操作时,在列表项内部(例如,firsr项目) Qte = {{product.Qte}}
它使我得到Qte = 5
但是当我想将数量放到选择上时,它不适用于ng-init:
<select
ng-model="selectedQte"
ng-init="selectedQte = {{product.Qte}}"
ng-options="selectedQte for selectedQte in range('10')"
ng-change="product.Qte = selectedQte">
</select>
我努力了 :
ng-init="selectedQte = product.Qte"
和:
ng-init="selectedQte = {{product.Qte}}"
但是什么也没发生。 当我尝试过:
ng-init="selectedQte = 3"
我的3
个选择中有3
个。
但我想5
上的第一个, 2
对第二个和7
上的第三位。
需要任何帮助,谢谢!
看起来问题在于JSON中Qte
的dataType
是string
,并且为range创建的array
是number
序列。 您需要将响应中的JSON转换为number
而不是string
才能解决问题。 您可以将Product.Qte
直接绑定到ng-model
。
标记
<select ng-model="product.Qte"
ng-options="selectedQte for selectedQte in range('10')">
</select>
这应该工作:
<select ng-model="selectedQte" ng-init="selectedQte = panier.Produits[0]"
ng-options="item.Name for item in panier.Produits">
</select>
您的ng-model包含选定的值,但是ng-option应该遍历您的列表。 在您的html中,两种情况都使用selectedQte。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.