简体   繁体   English

ng-init来自ng-repeat

[英]Ng-init from an ng-repeat

I have a list of products, you can see the JSON : 我有一个产品列表,您可以看到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"
            }
        ]
    };

I do a list with ionic to retrieve the products and it all works. 我用ionic列出了要检索的产品,所有这些都可以使用。

Inside of the list Item when I do (for example the firsr item) Qte = {{product.Qte}} it gives me Qte = 5 当我执行操作时,在列表项内部(例如,firsr项目) Qte = {{product.Qte}}它使我得到Qte = 5

But when I want to put the quantity on a select it doesn't work with the ng-init : 但是当我想将数量放到选择上时,它不适用于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>

I have tried : 我努力了 :

ng-init="selectedQte = product.Qte"

and : 和:

ng-init="selectedQte = {{product.Qte}}"

But nothing happens. 但是什么也没发生。 When I have tried : 当我尝试过:

ng-init="selectedQte = 3"

I have a 3 on my 3 selects. 我的3个选择中有3个。

But I want 5 on the first one, 2 on the second and 7 on the third. 但我想5上的第一个, 2对第二个和7上的第三位。

Any help is needed, thanks ! 需要任何帮助,谢谢!

Looks like the problem is with the dataType of the Qte in the JSON is string & the array created for range is sequence of number . 看起来问题在于JSON中QtedataTypestring ,并且为range创建的arraynumber序列。 You need to convert the JSON coming from response to number instead of string will do the trick. 您需要将响应中的JSON转换为number而不是string才能解决问题。 You could directly bind the Product.Qte to your ng-model . 您可以将Product.Qte直接绑定到ng-model

Markup 标记

<select ng-model="product.Qte"
    ng-options="selectedQte for selectedQte in range('10')">
</select>

This should work: 这应该工作:

<select ng-model="selectedQte" ng-init="selectedQte = panier.Produits[0]"
ng-options="item.Name for item in panier.Produits">
</select>

Your ng-model contains the selected value, however ng-option should iterate over your list. 您的ng-model包含选定的值,但是ng-option应该遍历您的列表。 In your html you use selectedQte for both cases. 在您的html中,两种情况都使用selectedQte。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM