2016-12-10 2 views
0

Ich habe dies in meiner ‚View-Datei‘Angular ng Optionen Standardauswahlwert

<select class="form-control" id="selection" ng-model="currentSelected" 
         ng-options="selection.id as selection.name for selection in selections track by selection.id"></select> 

Und dies in meinem Controller

$scope.currentSelected = 1; 
$scope.selections = [ 
    { 
     id: 1, 
     name: 'Name #1' 
    }, 
    { 
     id: 2, 
     name: 'Name #2' 
    } 
]; 

jedoch die Auswahl von Optionen sind leer und ich möchte es sein In diesem Beispiel "Name # 1". Was fehlt mir hier?

+0

entfernen 'Spur von selection.id'. –

+0

Vielen Dank, @JBNizet –

Antwort

0

Spur von funktioniert gut mit Array. Wenn Sie möchten, dass es ID ist, dann sollten Sie selection.id as selection.name for selection in selections

entfernen track by selection.id und es sollte gut funktionieren.

DEMO

var app = angular.module('todoApp', []); 
 

 
app.controller("dobController", ["$scope", 
 
    function($scope) { 
 
    $scope.currentSelected = 1; 
 
    $scope.selections = [{ 
 
     id: 1, 
 
     name: 'Name #1' 
 
    }, { 
 
     id: 2, 
 
     name: 'Name #2' 
 
    }]; 
 
    } 
 
]);
<!DOCTYPE html> 
 
<html ng-app="todoApp"> 
 

 
<head> 
 
    <title>To Do List</title> 
 
    <link href="skeleton.css" rel="stylesheet" /> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script> 
 
    <script src="MainViewController.js"></script> 
 
</head> 
 

 

 
<body ng-controller="dobController"> 
 
    <select class="form-control" id="selection" ng-model="currentSelected" ng-options="selection.id as selection.name for selection in selections"></select> 
 
</body> 
 

 
</html>