对角上的动态选项删除空白选项对象

问题描述:

我有这样的代码对角上的动态选项删除空白选项对象

<select ng-model='item.data.choice' > 
    <option ng-if='item.data.simple_allow_blank == false' ng-show='choice.name' ng-repeat='choice in item.data.simple_choices' value="{{choice.name}}" ng-disabled="$parent.controllerAction == 'show'">{{choice.name}}</option> 
</select> 

它显示了系统管理员在下拉框中输入的3种选择。它显示它没有空白的选择。

问题是如果我想要放一个默认值(例如,choice[0]),它会添加一个空白值。

另外,从ng-repeatthis question转换为ng-options时。

我有4个选择1,2,3和空白我想删除空白我跟随在我发布的链接上发现的指南/答案我的问题,我的部分差异是,我有一个动态列表并且当该列表是新创建的。使用ng-option时,它永远不会包含空白,但它解决了我的问题,从动态列表中获得初始默认值,如果我使用ng-repeat,我没有初始值,但删除选项的空白。我想删除空白选项,同时从动态列表中获取初始值。

我可以设置默认值,但不能删除空白选项。

任何人都可以帮忙吗?谢谢。

+0

https://*.com/questions/20738953/angular-js-remove-blank-option-from-select-option – Edison

+0

不是一个即时通讯:( – shimukawa

+0

你的意思是你想要ex.4选项包括空白选项一旦你选择了选项,那么它应该删除,对吗? – Nikita

You have to filter data in js file and create a new array that you will render it in html.let say you are getting data in variable **item.data.simple_choices**(as you have written it in ng-repeat)then your function would be. 

var data = item.data.simple_choices; 
var filteredArray = []; 
data.filter(function(item){ 
    if(item.name!=''){ 
filteredArray.push(item) 
} 
}) 
+0

不,这是不是这种情况,我有一个o在其中由admin动态创建的选项。 – shimukawa

经过几小时的水蛭网络和大量的反复试验,我终于得到了正确的答案。

<select ng-model='model'> 
     <!-- correct binding --> 
      <option ng-show='choice.name' ng-repeat='choice in object' value="{{choice.name}}" ng-selected='model = object[0].name'>{{choice.name}}</option> 
     </select> 

代码很简单,但这种格式很有用,因为对象是一个具有列表值的对象。正如你所看到的,我将它分配给模型,因为模型是在视图中使用的变量。 [0]是列表的索引号。您可以使用值或名称,具体取决于对象上存在的参数。