【问题标题】:How to add selected attribute to ng-repeated dropdown form?如何将选定属性添加到 ng-repeated 下拉表单?
【发布时间】:2015-05-20 11:40:11
【问题描述】:

我有一个基于一些 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>

^ 如果这是 object 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';
    }
}

你会怎么做?

【问题讨论】:

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


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 2017-09-27
      • 2016-04-17
      相关资源
      最近更新 更多