【问题标题】:How to remove empty entry from ng-repeat Angulst JS [duplicate]如何从 ng-repeat Angulst JS 中删除空条目 [重复]
【发布时间】:2018-08-20 21:57:37
【问题描述】:

嗨,这是我的 html 代码

 <select style="width: 199px;margin-top: -9px;" id="ddlTypeCode" ng-model="selectedCountryCode" ng-change="locationChange(selectedCountryCode)" name="ddlTypeCode" class="form-control">
                    <option ng-repeat="Country in CountryList track by $index" value="{{Country}}"
                            ng-selected="Country == 'India'">
                        {{Country}}
                    </option>

CountryList 没有任何空值,但在 UI 下拉菜单中显示一个空条目。请建议

【问题讨论】:

    标签: html angularjs angularjs-ng-repeat


    【解决方案1】:

    在使用 ng-repeat 的数组时,这在 angularjs 中很常见。

    很简单,只需添加一个&lt;option&gt; 标记和style="display:none"&lt;option style="display:none"&gt;&lt;/option&gt; 它完成了这项工作。

    var app = angular.module("myApp", []);
    
    app.controller('testCtrl', ['$scope', function ($scope) {
     $scope.CountryList = ["India", "China", "Japan"]; 
       
       
    }]);
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="testCtrl">
        
    <select style="width: 199px;margin-top: -9px;" id="ddlTypeCode" ng-model="selectedCountryCode" ng-change="locationChange(selectedCountryCode)" name="ddlTypeCode" class="form-control">
              <option  style="display:none;"></option>
             <option ng-repeat="Country in CountryList " value="{{Country}}"ng-selected="Country == 'India'">
                            {{Country}}
                        </option>
            
    
    </div>

    希望对你有帮助.. 干杯!

    ~尼基尔

    【讨论】:

    • track by $index 用于如果有任何重复条目,我想这里不需要。
    【解决方案2】:

    当传递给 ng-options 的一组选项中不存在 ng-model 引用的值时,将生成空选项。

    看看Why does AngularJS include an empty option in select?

    【讨论】:

    • @Adriani6 为什么投反对票?因为缺乏声誉无法评论。我发布答案的速度比你快
    • 你不能只发布这个作为答案。如果每个人都这样做,董事会将一团糟。当您有足够的代表时,您可以发布 cmets。该系统是这样设计的,您应该接受并遵循这一点。您不必使用 cmets。声望为 15 时,您会看到一个“标记”选项,我们使用该选项将问题标记为重复问题,以保持论坛井井有条和整洁。这一切都需要耐心:)
    • 如果我不能发表评论我如何获得声誉?
    • 通过回答问题,您可以同时进行。我知道这听起来很难,但我们都必须从同一个地方开始 :)
    • 现在明白了
    猜你喜欢
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 2015-11-05
    • 1970-01-01
    • 2017-01-15
    相关资源
    最近更新 更多