清空下拉列表中的第一个元素 [英] Empty first element in dropdown list

查看:31
本文介绍了清空下拉列表中的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 AngularJS 的初学者,目前我正在 Django 中开发一个 Web 应用程序,在那里我使用 AngularJS 作为前端部分,我可以说.我的问题是用范围内的对象填充的下拉列表总是以空白元素开头(如果我从列表中选择一个,问题就消失了).这会产生问题,因为如果用户没有选择任何内容,POST 请求通常将不再起作用.我想知道如何拥有诸如预选值之类的东西或类似的东西.这是我的代码的一部分:

I am quite a beginner with AngularJS and currently I am working on a web application in Django where I use AngularJS for the frontend part i can say. My problem is that the dropdown list which is populated with objects from a scope always start with a blank element (if i select one from the list, the issue is gone). This create problems because if the user doesn't select anything the POST request normally it will not work anymore. I would like to know how to have something like a preselected value or something similar. Here's part of my code:

选择标签:

<select id="sel" class="input-block-level" ng-model="list_category">
            <option ng-repeat="obj in list_categories.data" value="{{obj.id}}">{{obj.name}}</option>
            <option value="Other">Other</option>
        </select>

$scope.list_categories:

$scope.list_categories:

var listcategoryPromise = ListCategory.get();
    listcategoryPromise.then(function(response) {
        $scope.list_categories = {
            meta : response.data.meta,
            data : response.data.objects
        };
    });

推荐答案

使用指令 ng-options 并从 'Other' 选项中删除 value,像这样:

Use the directive ng-options and remove the value from the 'Other' option, like this:

<select id="sel" class="input-block-level" ng-model="list_category" ng-options="obj.id as obj.name for obj in list_categories.data">    
    <option value="">Other</option>
</select>

这可确保如果 list_category 为空(未选择任何条目),则选择其他"选项(默认情况下).

This ensures that if list_category is empty (no entry selected), the 'Other' option is selected (by default).

jsFiddle:http://jsfiddle.net/bmleite/gkJve/

这篇关于清空下拉列表中的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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