如何将选定的属性添加到ng-repeated下拉表单中?

时间:2015-03-17 20:45:52

标签: javascript angularjs angularjs-ng-repeat angular-ng-if

我有一个基于一些JSON数据构建多个小部件的表单。在该表单的一部分是选择下拉列表,并且某些项目默认选择了不同的选项。

即:

object 1 {
    tag: "products"
}

ng-repeat小部件中的选择下拉列表

<select class="btn-success form-control">
    <option value="companies">companies</option>
    <option value="news">news</option>
    <option value="people">people</option>
    <option value="products">products</option>
</select>

^如果这是对象1 ,我需要使用products选项来获取selected属性。


到目前为止,我所尝试的内容并没有奏效,但您可以看到我的想法:

HTML

ng-repeat="stuff in stuffs"...

<select class="btn-success form-control">
    <option value="companies">companies</option>
    <option ng-if="widget.selectedTag(stuff.tag)" value="news">news</option>
    <option value="people">people</option>
    <option value="products">products</option>
</select>

控制器

this.selectedTag= function(s) {
    console.log(s);

    if (s = 'news'){
        return 'selected';
    }
}

你会怎么做?

1 个答案:

答案 0 :(得分:1)

在这里找到答案:Initializing select with AngularJS and ng-repeat

<option ng-selected="{{operator.value == filterCondition.operator}}"
        ng-repeat="operator in operators"
        value="{{operator.value}}">

所以在我的情况下:

<option value="products"
        ng-selected="{{stuff.tag == 'products'}}">products</option>