棱角分明,选择列表中的第一个项目是空的 [英] Angular, select list first item is empty

查看:100
本文介绍了棱角分明,选择列表中的第一个项目是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我填充我的选择列表中像这样 -

I am populating my select list like so -

<select class="selectLevel0" ng-model='scope1' ng-change='scope1Change()' 
            ng-options='obj.name for obj in array track by obj.id'>
</select>

下面是$ HTTP背后:

Here is the $http behind it :

   //populate  scopes
    $http.post("/listScopes").success(function(data){
        $scope.array = data.scopes;
    });

正在发生的事情是 - 我得到一个没有名字和一个值=第一选择项? (其余填充罚款)。我似乎无法弄清楚它为什么这样做。任何帮助将是非常美联社preciated。谢谢!

What is happening is - I am getting a first select item that has no name and a value="?" (the rest populate fine). I can't seem to figure out why it's doing this. Any help would be much appreciated. Thanks!

推荐答案

原因是因为你的ngModel是最有可能未分配或不与ID正确分配,角创建一个空白选项,并为选定的显示。

Reason is that since your ngModel is most possibly not assigned or not assigned properly with the id, angular creates a blank option and display that as selected.

您既可以添加一个默认选项: -

You could either add a default option:-

<select class="selectLevel0" ng-model='scope1' ng-change='scope1Change()' 
            ng-options='obj.name for obj in array track by obj.id'>
 <option value="">Please choose a level</option>
</select>

或者与你的控制器默认值设为您的ngModel或将其设置在 NG-INIT 。我会做的控制器: -

Or set your ngModel with a default value in your controller or set it in ng-init. I would do it in the controller:-

 $scope.scope1 = "id"

这篇关于棱角分明,选择列表中的第一个项目是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆