Javascript selectbox中的选定值未设置为angularjs
我正在尝试在选择框中设置默认选定值:Javascript selectbox中的选定值未设置为angularjs,javascript,angularjs,Javascript,Angularjs,我正在尝试在选择框中设置默认选定值: <select class="form-control input-sm2" ng-init="selectedSupercustomer = superkundOptions[5]" ng-model="selectedSupercustomer" ng-options="item as item.namn for item in superkundOptions" ng-change="onChangeSuperCustomer(selectedS
<select class="form-control input-sm2" ng-init="selectedSupercustomer = superkundOptions[5]" ng-model="selectedSupercustomer" ng-options="item as item.namn for item in superkundOptions" ng-change="onChangeSuperCustomer(selectedSupercustomer)">
这不管用。第一行仍然为空。您可以尝试通过控制器而不是ng init将其放入 控制器
$scope.selectedSupercustomer = $scope.superkundOptions[5];
JS
<select class="form-control input-sm2" ng-model="selectedSupercustomer" ng-options="item as item.namn for item in superkundOptions" ng-change="onChangeSuperCustomer(selectedSupercustomer)">
在您的示例中,superkundOptions
数组仅包含2项,您正在尝试获取第五项。Bug
您的数组只有两个项,所以不能将索引放在高于一个的位置
解决
选项1
您需要更改ng selected
而不是ng init
,还需要更改数组索引
Html代码
你可以看看这把小提琴
<select></select>
我认为您应该这样做。
ng init=“selectedSupercustomer=superkundOptions[0]”
与数组superkundOptions一样,第5位没有元素。@Anita:谢谢:)非常感谢您的回答:)
<select class="form-control input-sm2" ng-selected="superkundOptions[0]" ng-
model="selectedSupercustomer" ng-options="item as item.namn for item in
superkundOptions" ng-change="onChangeSuperCustomer(selectedSupercustomer)">
$scope.superkundOptions = [{ id: 5, namn: 'Halmstad Bågen / Pilen' }, { id: 6, namn: 'fisk Bågen / Pilen' }];
$scope.selectedSupercustomer=$scope.superkundOptions[0]// **put 0 or 1 only**
<select class="form-control input-sm2" ng-
model="selectedSupercustomer" ng-options="item as item.namn for item in
superkundOptions" ng-change="onChangeSuperCustomer(selectedSupercustomer)">
<select></select>