如何根据AngularJS

时间:2015-07-14 15:52:16

标签: angularjs

我有这样的JSON数据:

[
{
    "id": "1",
    "name": "abc",
    "image": "abc.png",
    "subcategories": [
        {
            "scid": "1",
            "scname": "abc1"
        },
        {
            "scid": "2",
            "scname": "abc2"
        },
        {
            "scid": "3",
            "scname": "abc3"
        }
    ]
},
{
    "id": "2",
    "name": "xyz",
    "image": "xyz.png",
    "subcategories": [
        {
            "scid": "4",
            "scname": "xyz1"
        },
        {
            "scid": "5",
            "scname": "xyz2"
        },
        {
            "scid": "6",
            "scname": "xyz3"
        }
    ]
}
]

并尝试填写类别和子类别:

            <tr>
                <div class="form-group">
                    <TD>Category Name</TD>
                    <td>
                        <select class="form-control" ng-model="NewProduct.category" ng-options="category as category.name for category in Categories | orderBy:['name']" required></select>
                    </td>   
                </div>

            </tr>
            <tr>
                <div class="form-group">
                    <TD>Sub Category Name</TD>
                    <td>
                        <select class="form-control" ng-model="NewProduct.subcategory" ng-options="subcategory as subcategory.name for subcategory in Categories.subcategories | orderBy:['scname'] | filter:{ 'id' : NewProduct.category.id }:true" required></select>
                    </td>
                </div>
            </tr>

虽然类别填写得很好,但在选择类别后没有填充子类别。

有人可以帮我解决这里的错误吗?

1 个答案:

答案 0 :(得分:1)

在您的第二个ng选项中,您说:

subcategory as subcategory.name for subcategory in Categories.subcategories

但是Categories看起来可能是一个数组,因此不会有.subcategories属性。相反,我认为您打算在第一次选择时使用ngModel中的类别。

ng-model="NewProduct.category" 

这会让你:

subcategory as subcategory.name for subcategory in NewProduct.category.subcategories