2016-12-10 294 views
0

我有這在我的「查看文件」角默認NG選項選擇值

<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> 

這在我的控制器

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

然而,選擇選項是空白,我希望它是在這個例子中「名稱#1」。我在這裏錯過了什麼?

+0

通過selection.id'刪除'軌道。 –

+0

非常感謝@JBNizet –

回答

0

跟蹤與數組很好。如果你想要它是由id,那麼你應該使用selection.id as selection.name for selection in selections

刪除track by selection.id它應該工作正常。

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>