gpt4 book ai didi

javascript - Angular ng-选项 : compare by id

转载 作者:行者123 更新时间:2023-12-03 11:02:31 24 4
gpt4 key购买 nike

我可以将选择的 ng-model 设置为对象的键吗?在下面的示例中,默认情况下应选择“item1”。(我不想通过引用比较对象)

<div ng-controller='TestCtrl'>
<select
ng-model='selectedId'
ng-options='value for (key, value) in items'>
</select>
</div>
var app = angular.module('myApp', []);

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

$scope.selectedid = 1;

$scope.items = {
1: 'item1',
2: 'item2'
}

});

这是 jsfiddle:http://jsfiddle.net/h7yb4bys/

最佳答案

结账 http://jsfiddle.net/oj884ofm/

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

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

$scope.selectedId = '1';

$scope.items = {
1: 'item1',
2: 'item2'
}

});

关于javascript - Angular ng-选项 : compare by id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28021832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com