2

我正在使用ng-option來更新我的模型數據。但是,當我第一次從下拉列表中選擇最後一個選項時,我無法看到我的選擇。但是,在我的模型中,代碼保存了所選項目。AngularJS:帶鍵值的ng-option - 更新對象

我的HTML:

<select class="form-control" name="myField" ng-model="myField" ng-options="key as key for (key , value) in fieldFieldOptions" ng-required="myAllFiles=='NO'"/></select> 

controller- 
$scope.fieldFieldOptions = []; 
$scope.fieldFieldOptions = result.data;[contains drop down list] 

$scope.myFIeld包含從下拉列表中選擇我的項目。

我在這個特定情況下做錯了什麼?

+0

提供plunker –

+0

是什麼'result.data'的結構你的代碼? – yarons

+0

result.data:對象 \t \t \t \t姓: 「6」 \t \t \t \t名字: 「8」 \t \t \t \t ID: 「19」 \t \t \t \t選擇: 「10」 – JOGO

回答

0

這裏是一個工作片段與ngOptions:

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

 
app.controller('MainCtrl', function($scope) { 
 

 
    $scope.myField; 
 

 
    result = {}; 
 
    result.data = { 
 
    "key1": { 
 
     firstName: "6", 
 
     lastName: "8", 
 
     Id: "19", 
 
     select: "10" 
 
    }, 
 
    "key2": { 
 
     firstName: "1", 
 
     lastName: "11", 
 
     Id: "39", 
 
     select: "11" 
 
    }, 
 
    "key3": { 
 
     firstName: "2", 
 
     lastName: "22", 
 
     Id: "29", 
 
     select: "12" 
 
    } 
 
    }; 
 

 
    $scope.fieldFieldOptions = result.data; 
 

 
});
<!DOCTYPE html> 
 
<html ng-app="myApp"> 
 

 
<head> 
 
    <meta charset="utf-8" /> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script src="https://code.angularjs.org/1.4.8/angular.js"></script> 
 
    <script src="app.js"></script> 
 
</head> 
 

 
<body ng-controller="MainCtrl"> 
 

 
    <div> 
 
    <select class="form-control" name="myField" ng-model="myField" 
 
    ng-options="key as key for (key, item) in fieldFieldOptions" 
 
    ng-required="myAllFiles=='NO'"> 
 
    </select> 
 
    </div> 
 
    <pre>fieldFieldOptions = {{fieldFieldOptions | json}}</pre> 
 
    <pre>myField = {{myField | json}}</pre> 
 

 
</body> 
 

 
</html>

+0

result.data的結構類似於你的(關聯數組)嗎? – beaver

+0

是的..它的工作..謝謝你.. :) – JOGO

+0

歡迎您...請投票;-) – beaver

-1

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

 
app.controller('MainCtrl', function($scope) { 
 

 
    $scope.myField; 
 

 
    result = {}; 
 
    result.data = { 
 
     firstName: "6", 
 
     lastName: "8", 
 
     Id: "19", 
 
     select: "10" 
 
    }; 
 

 
    $scope.fieldFieldOptions = result.data; 
 

 
});
<!DOCTYPE html> 
 
<html ng-app="myApp"> 
 

 
<head> 
 
    <meta charset="utf-8" /> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script src="https://code.angularjs.org/1.4.8/angular.js"></script> 
 
    <script src="app.js"></script> 
 
</head> 
 

 
<body ng-controller="MainCtrl"> 
 

 
    <div> 
 
    <select class="form-control" name="myField" ng-model="myField" 
 
    ng-options="key as key for (key, item) in fieldFieldOptions" 
 
    ng-required="myAllFiles=='NO'"> 
 
    </select> 
 
    <pre>myField = {{myField | json }}</pre> 
 
    </div> 
 
</body> 
 

 
</html>