Angular的ng-options工作不正确Angularjs

时间:2015-05-14 08:25:12

标签: javascript angularjs

Angular的ng-options工作不正确。

必须像这样选择:

<option value="1" label="Not available">Not available</option>
<option value="3" selected="selected" label="Available">Available</option>

但事实并非如此。值键不匹配,选定的标签不起作用。

我在这里做错了什么?

来自db的产品型号对象

Object {id: "7", name: "Some name", category_id: "1", img_big: "uploads/products/5552f2ba31694@big.jpeg", img_thumb: "uploads/products/5552f2ba31694@thumb.jpeg"…}

isAvailable object

$rootScope.isAvailableScope = [
    {
        status: 1,
        name  : 'Not available'
    },
    {
        status: 3,
        name  : 'Available'
    }
]

HTML

<select class="form-control"
        ng-model="product.is_available"
        ng-options="isAvailable.status as isAvailable.name for isAvailable in isAvailableScope"
        required>
    <option value="">Choose</option>
</select>

1 个答案:

答案 0 :(得分:0)

$rootScope更改为$scope。您在控制器内并且在其范围内。

$rootScope.isAvailableScope = [
    {
        status: 1,
        name  : 'Not available'
    },
    {
        status: 3,
        name  : 'Available'
    }
]

到...

$scope.isAvailableScope = [
    {
        status: 1,
        name  : 'Not available'
    },
    {
        status: 3,
        name  : 'Available'
    }
]

<强> FIDDLE