gpt4 book ai didi

javascript - 在 angularjs 中使用 'track by' 选择默认下拉列表不起作用

转载 作者:行者123 更新时间:2023-12-02 13:53:55 25 4
gpt4 key购买 nike

我使用track by来避免附加到value的奇怪值::string,但是当我默认选择下拉列表时,下拉列表没有被选中..这是我的笨蛋

Example Plunker

最佳答案

(function(angular) {
'use strict';
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: '2' //This sets the default value of the select in the ui
};
}]);
})(window.angular);
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-select-with-default-values-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="defaultValueSelect">
<div ng-controller="ExampleController">
<form name="myForm">
<label for="mySelect">Make a choice:</label>


<select ng-model="data.selectedOption">
<option ng-repeat="option in data.availableOptions track by option.id" value="{{option.id}}">{{ option.name}}</option>
</select>
</form>
<hr>
<tt>option = {{data.selectedOption}}</tt><br/>
</div>
</body>
</html>

演示的工作片段

请更改您的<select>标签如下

<select ng-model="data.selectedOption"> 
<option ng-repeat="option in data.availableOptions track by option.id" value="{{option.id}}">{{ option.name}}</option>
</select>

关于javascript - 在 angularjs 中使用 'track by' 选择默认下拉列表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40804844/

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