从 angularjs 下拉列表中删除空白选项

Remove blank option from angularjs dropdown

下面的函数来自 angularjs 控制器并为下拉列表动态设置值,它可以工作,但第一个选项始终为空白,值为“?”。我应该如何修改功能以删除空白选项或将所选选项设置为项目[0]?

--编辑--

我没有发布完整的控制器,它来自第 3 方包,控制器显示下拉值更改时调用的函数 u。

有 2 个下拉列表,当一个更改时,另一个应使用新数据更新,这部分有效,但新数据具有上述额外的空白选项。

谢谢

   function u() {
          var subs;
          if (n.model.alias == "group") {
              var selectedGroup = document.getElementById("group").value;
              subs = angular.element(document.getElementById('subGroup')).controller();

              $.ajax({
                  url: "/umbraco/Api/ContentmentCustomApi/GetSubGroups",
                  type: "GET",
                  cache: false,
                  async: false,
                  data: { selectedGroup: selectedGroup }
              }).then(function (data) {
                  subs.items = data;
                });
          };
  }
      <div class="contentment" ng-class="vm.uniqueId" ng-controller="Umbraco.Community.Contentment.DataEditors.DropdownList.Controller as vm">     
        <select id="{{model.alias}}"             class="umb-dropdown"             lk-html-attributes="vm.htmlAttributes"             ng-model="model.value"           ng-change="vm.change()"       ng-options="item.value as item.name disable when item.disabled for item in vm.items">         
    
        </select> 
      </div>

当您设置 ngOptions 时,预计会重新分配 model.value 的值。

根据您问题中的代码得出类似的结果

subs.items = data;
n.model.value = subs.items[0];

select的第一个选项为空的原因是ng-model引用的值不存在于ng-option的列表中。

请注意,有时值相同但引用不同。

进一步阅读的内容:

https://docs.angularjs.org/api/ng/directive/ngOptions

By default, ngModel watches the model by reference, not value. This is important to know when binding the select to a model that is an object or a collection.

One issue occurs if you want to preselect an option. For example, if you set the model to an object that is equal to an object in your collection, ngOptions won't be able to set the selection, because the objects are not identical. So by default, you should always reference the item in your collection for preselections, e.g.: $scope.selected = $scope.collection[3].