<p>选项,{{selected}}</p>
<select ng-model="selected" ng-options="item.id as item.name for item in selectData">
<option value="">-- 请选择 --</option>
</select>
$scope.selected = 1004;
$scope.selectData = [
{id:1001,name:"零零一"},
{id:1002,name:"零零二"},
{id:1003,name:"零零三"},
{id:1004,name:"零零四"}
];
如何取得選取的那一列資料。 selected 只能取得id值,但是我想要這一列的值。且不干擾透過id值預設選取某一項。比如說我透過id:1004選取了這一項,我不單單要id值還要這一列的所有中。
我想大声告诉你2017-05-15 17:13:43
你不覺得你的需求有矛盾嗎,給ng-model賦值id值,又期望ng-model的值是物件?
如果想要最終ng-model是一列,但是你只能給選取某項的id值(應該是你在頁面編輯中遇到的),可以透過給定id先過濾出對應項,並賦值為selected。且ng-options表達式需要改成 item as item.name for item in selectData track by item.id
$scope.selectData = [
{id:1001,name:"零零一"},
{id:1002,name:"零零二"},
{id:1003,name:"零零三"},
{id:1004,name:"零零四"}
];
$scope.selected = $scope.selectData.filter(item => item.id === 1004)[0];
<select ng-model="selected" ng-options="item as item.name for item in selectData track by item.id">
<option value="">-- 请选择 --</option>
</select>