一旦我使用ng-options
进行选择,我无法让dafault选择对嵌套的json对象起作用。
一旦我有一个更复杂的json并且select应该处理一个子对象,我的select不会默认选择正确的值。
将test = {"id":3,"title":"Test","product":{"id":4,"name":"Test1"}}
作为我的ng-model test.product
和。{
[{
"id": 4,
"name": "Test1"
}, {
"id": 5,
"name": "Test2"
}]
作为我的选择选项。 (见http://embed.plnkr.co/mpnislw77UBSEdHl4UKN/) 我似乎无法弄清楚如何促进默认选择。
如果你使用track by item.id它可以工作 - http://embed.plnkr.co/mpnislw77UBSEdHl4UKN。由于ng模型嵌套在iself中,因此标记的答案并不十分明显。但它包含正确的信息。
答案 0 :(得分:1)
您的代码唯一的问题是,您已将新对象分配给$scope.test.product
,并且您将其用作下拉列表的ng-model
。
这使得AngularJS无法在$scope.testarray
的可能值中找到它。 AngularJS将通过引用来比较两个对象,当您将新对象分配给$scope.test.product
时,它会破坏。
要使其正常运行,请按以下方式更改$scope.test
:
$scope.test = {
"id": 3,
"title": "Test",
"product": $scope.testarray[1]
}
答案 1 :(得分:0)
试试这个,希望它会有所帮助
<option value="" ng-if="false"></option>
答案 2 :(得分:0)
这是如何使用ngOptions选择并设置默认值
示例强>
CefSharp
&#13;
angular.module('defaultValueSelect', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.data = {
availableOptions: [
{id: '1', name: 'Option A'},
{id: '2', name: 'Option B'},
{id: '3', name: 'Option C'}
],
selectedOption: {id: '3', name: 'Option C'} //This sets the default value of the select in the ui
};
}]);
&#13;