[英]How to keep first option selected in oi-select angularjs
Plunker to try I am using oi-select library to select multiple options, I want the first value from data should be selected by default, as soon as the data is available. 尝试一下,我正在使用oi-select库选择多个选项,我希望数据可用时,默认情况下应选择数据中的第一个值。 html code
HTML代码
<oi-select multiple ng-init="selectedNewRelease=releaseList[0]"
oi-options="item.releaseId for item in releaseList" ng-model="selectedNewRelease"
placeholder="Select Releases"
oi-select-options="{dropdownFilter: 'myDropdownFilter'}" id="selectedReleases"
ng-change="onReleaseChange();toggleCol();"></oi-select>
I tried with ng-init, by assigning as below : 我尝试通过指定如下ng-init:
$scope.selectedNewRelease=$scope.releaseList[0];
This actually works but as soon as I select one or more option it removes default selected option. 这实际上有效,但是一旦我选择一个或多个选项,它就会删除默认的选定选项。 Any idea any link please.
任何想法任何链接,请。 link for oi-select
oi-select的链接
when setting default value for oi-select
(multiple) with a Object not Array, selecting new item will lead to overwriting the original value. 当为“非数组对象”设置
oi-select
(多个)的默认值时,选择新项目将导致覆盖原始值。 So you should init it with an Array
.Something like this: 因此,您应该使用
Array
初始化它。
ng-init="selectedNewRelease=[releaseList[0]]
UPDATE: 更新:
oi-select
used ngModel
to achieve two-way-databinding
, and since angular won't trigger change if the instance
of the array is not changed(means ng-change won't fire without instance changed). oi-select
使用ngModel
实现two-way-databinding
,并且如果不更改数组的instance
,则angular不会触发更改(意味着ng-change在不更改实例的情况下不会触发)。
So after push new items, you should do something like this: 因此,在推送新项目之后,您应该执行以下操作:
$scope.selectedNewRelease = $scope.selectedNewRelease.slice(); // create new instance for the array.
try this 尝试这个
ng-init="selectedNewRelease=releaseList[0]"
instead of ng-init="releaseList[0]"
ng-init="selectedNewRelease=releaseList[0]"
而不是ng-init="releaseList[0]"
Edit: 编辑:
Or remove ng-init="selectedNewRelease=releaseList[0]"
on oi-select and assign the value in controller after load the releaseList
data 或在oi-select上删除
ng-init="selectedNewRelease=releaseList[0]"
并在加载releaseList
数据后在控制器中分配值
like 喜欢
function controller($scope)
{
Loaddropdown()
{
$http().success(function(data){
$scope.releaseList = data;
$scope.selectedNewRelease =$scope.releaseList[0]//or $scope.selectedNewRelease=$scope.releaseList[0].Id// which id means what's your value
})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.