Angularjs 向ui选择选项添加静态选项

Angularjs 向ui选择选项添加静态选项,angularjs,ui-select,Angularjs,Ui Select,在代码中,以下选项是从数据库动态添加的 <ui-select ng-model="model.people"> <ui-select-match>{{$select.selected.name}}</ui-select-match> <ui-select-choices repeat="value.id as value in options | filter: $select.search"> <div ng

在代码中,以下选项是从数据库动态添加的

<ui-select ng-model="model.people">
    <ui-select-match>{{$select.selected.name}}</ui-select-match>
    <ui-select-choices repeat="value.id as value in options | filter: $select.search">
        <div ng-bind-html="value.name | highlight: $select.search"></div>
    </ui-select-choices>
</ui-select>

{{$select.selected.name}

我的问题是:如何将选项“全部”手动添加到下拉列表中。有什么方法可以做到这一点吗?

最简单的方法是在从服务器获取数据后将其添加到组件/控制器的
选项中。手动将其作为数组中的第一项推送

options.splice(0,0,{id: -1, name: 'all'});

最简单的方法是在从服务器获取数据后将其添加到组件/控制器的
选项中。手动将其作为数组中的第一项推送

options.splice(0,0,{id: -1, name: 'all'});

您可以将一个
静态
对象添加到控制器本身的JSON
选项中

使用Array方法将一个或多个元素添加到数组的开头,并返回新数组的新长度

演示

var-app=angular.module('app',['ui.select']);
应用程序控制器(“myCtrl”,函数(){
vm=这个;
vm.values=[{
"关键":22,,
“值”:“凯文”
}, {
“关键”:24,
“值”:“Fiona”
}];
unshift({'key':''value':'Static'});
});

{{$select.selected.value}

您可以在控制器本身的JSON
选项中添加一个
静态
对象

使用Array方法将一个或多个元素添加到数组的开头,并返回新数组的新长度

演示

var-app=angular.module('app',['ui.select']);
应用程序控制器(“myCtrl”,函数(){
vm=这个;
vm.values=[{
"关键":22,,
“值”:“凯文”
}, {
“关键”:24,
“值”:“Fiona”
}];
unshift({'key':''value':'Static'});
});

{{$select.selected.value}
这应该可以:

<ui-select ng-model="model.people" theme="bootstrap">
<ui-select-match>{{ $select.selected }}</ui-select-match>
<ui-select-choices repeat="choice in ['option 1', 'option 2']">
    <span>{{ choice }}</span>
</ui-select-choices>

{{$select.selected}
{{choice}}

这应该可以:

<ui-select ng-model="model.people" theme="bootstrap">
<ui-select-match>{{ $select.selected }}</ui-select-match>
<ui-select-choices repeat="choice in ['option 1', 'option 2']">
    <span>{{ choice }}</span>
</ui-select-choices>

{{$select.selected}
{{choice}}

如果您想保持
选项列表的原样,并且只在下拉列表中添加静态选项,这将起作用:

<ui-select-choices repeat="value.id as value in [ staticOption ].concat(options)">

如果您想保持
选项列表的原样,并且只在下拉列表中添加静态选项,这将起作用:

<ui-select-choices repeat="value.id as value in [ staticOption ].concat(options)">


@NamburiAkhil-你说不工作是什么意思?请调试脚本并在此添加的代码行上放置断点。它被击中了吗?
选项
变量是否更改?您能看到新添加的数组项吗?一旦你这样做,屏幕上会发生什么?执行此代码后,是否传递相同的
options
实例?你确定你没有进行上述拼接,然后用服务器上的数据覆盖它吗?@NamburiAkhil-你说的
不工作是什么意思?请调试脚本并在此添加的代码行上放置断点。它被击中了吗?
选项
变量是否更改?您能看到新添加的数组项吗?一旦你这样做,屏幕上会发生什么?执行此代码后,是否传递相同的
options
实例?您确定没有执行上述拼接,然后用服务器上的数据覆盖它吗?