Javascript 在selectbox angularjs中设置选定值

Javascript 在selectbox angularjs中设置选定值,javascript,angularjs,Javascript,Angularjs,我有一个选择框,里面有从后端填充的选项 <tr> <td class="col-lg-4">Superkund</td> <td><select class="form-control input-sm2" ng-model="selectedSupercustomer" ng-options="item as item.namn for item in superkundOptions" ng-change="onChang

我有一个选择框,里面有从后端填充的选项

<tr>
    <td class="col-lg-4">Superkund</td>
    <td><select class="form-control input-sm2" ng-model="selectedSupercustomer" ng-options="item as item.namn for item in superkundOptions" ng-change="onChangeSuperCustomer(selectedSupercustomer)"></select></td>
</tr>

 $http.get($rootScope.appUrl + '/nao/abb/getSuperkundData/' + $rootScope.abbForm).success(function(data) {
            $scope.superkundOptions = data;
        });

但那是行不通的。有谁能帮我吗?

选择框可用于引用。这意味着,您必须将model
$scope.selectedSupercustomer
设置为$scope.superkundOptions[ID/INDEX\u OF_uu“fish”]。

使用
ng init
。例如,对于所选的第一个项目,
ng init

<select ng-init="selectedSupercustomer = superkundOptions[0]" ... ></select>


您可以看看这个示例

HTML:

CSS:


@AndréBauer:像这样:$scope.selectedSupercustomer=$scope.superkundOptions[0+'fish'];?因为这不起作用。它的意思是“找到值为“fish”的元素的id/索引”我应该把它放在哪里?@user500468不能添加没有代码的fiddle链接,所以我希望有这样的链接helps@Bummi做得好,你的修改让答案看起来很好:)@user500468你的错误是
superkundOptions[5]
。您尝试按id
5
进行选择,但必须按
superkundOptions
数组中必要元素的索引进行选择。如果要选择id=5的对象,您的
ng init
必须如下:
selectedsuperkundoptions=superkundOptions[0]
因为
superkundOptions[0]={id:5,namn:'Halmstad Bågen/Pilen'}
<select ng-init="selectedSupercustomer = superkundOptions[0]" ... ></select>
<div ng-app="myApp" ng-controller="SomeController">
    <div ng-repeat="Person in People">
        <div class="listheader">{{Person.firstName}} {{Person.lastName}}</div>
        <div class="listitem" ng-repeat="Choice in Person.Choices">
            {{Choice.Name}}: 
            <select 
                ng-model="Choice.SelectedOption"                 
                ng-options="choice.Name for choice in Choice.Options track by choice.ID"></select>
            {{Choice.SelectedOption.ID}}
        </div>
    </div>
</div>
var myApp = angular.module('myApp', []);
myApp.controller("SomeController", function($scope) {
    $scope.People = [{
        "firstName": "John",
        "lastName": "Doe",
        "Choices": [
            {
                "Name":"Dinner",
                "Options":[{Name:"Fish",ID:1}, {Name:"Chicken",ID:2}, {Name:"Beef",ID:3}],
                "SelectedOption":{Name:"Chicken",ID:2}
            },
            {
                "Name":"Lunch",
                "Options":[{Name:"Macaroni",ID:1}, {Name:"PB&J",ID:2}, {Name:"Fish",ID:3}],
                "SelectedOption":""
            }
        ],        
    }, {
        "firstName": "Jane",
        "lastName": "Doe"
    }];
});
body{
    font-family: verdana;
    font-size: 10pt;
}

.listheader{
    font-weight: bold;
    text-decoration: underline;
    padding-top: 5px;
}