gpt4 book ai didi

javascript - ng-selected 不更新下拉列表 angularjs

转载 作者:行者123 更新时间:2023-11-30 14:45:02 25 4
gpt4 key购买 nike

我一直在使用 AngularJS,在执行以下功能时,作为 select 元素的第一个子元素,我仍然得到一个空选项。

这是我的 HTML 代码片段

<select id="connectTV" aria-label="How many Dish TVs" ng-model="connectTVs.index" ng-change="onChangeMethod(connectTVs.index)" class="selector" aria-required="true">
<option ng-repeat="noOfTV in noOfTVs" ng-value="noOfTV.index" ng-selected="noOfTV.index == connectTVs.index">{{noOfTV.value}}</option>
</select>

JS代码

$scope.noOfTVs = [
{index: 0, value: 'How many Dish TVs?'},
{index: 1, value: 'Connect 1 Dish TV'},
{index: 2, value: 'Connect 2 Dish TVs'},
{index: 3, value: 'Connect 3 Dish TVs'},
{index: 4, value: 'Connect 4 Dish TVs'},
{index: 5, value: 'Connect 5 Dish TVs'},
{index: 6, value: 'Connect 6 Dish TVs'},
{index: 7, value: 'Connect 7 Dish TVs'},
{index: 8, value: 'Connect 8 Dish TVs'}
];


$scope.onChangeMethod = function(connectTVsIndex) {

// Doing some actions
$scope.connectTVs = $scope.noOfTVs[connectTVsIndex];
$window.sessionStorage.setItem("noOfTVSelected", $scope.connectTVs.index);
}

最后,这是生成的 HTML

<option value="? number:4 ?"></option>

enter image description here

获取空选项作为选择元素的第一个子元素。我哪里做错了?有人可以帮忙吗?

最佳答案

您可以使用 ng-options 进行选择而不是 ng-repeat。前者更好performance用于更大的阵列集并提供更大的灵 active 。

var app = angular.module('myApp', [])
app.controller('myCtrl', [
'$scope',
function($scope) {
$scope.noOfTVs = [{
index: 0,
value: 'How many Dish TVs?'
}, {
index: 1,
value: 'Connect 1 Dish TV'
}, {
index: 2,
value: 'Connect 2 Dish TVs'
}, {
index: 3,
value: 'Connect 3 Dish TVs'
}, {
index: 4,
value: 'Connect 4 Dish TVs'
}, {
index: 5,
value: 'Connect 5 Dish TVs'
}, {
index: 6,
value: 'Connect 6 Dish TVs'
}, {
index: 7,
value: 'Connect 7 Dish TVs'
}, {
index: 8,
value: 'Connect 8 Dish TVs'
}];
$scope.connectTVs=$scope.noOfTVs[0];
$scope.onChangeMethod = function() {
// Doing some actions
console.log($scope.connectTVs)
}

}
])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl">
<select id="connectTV" aria-label="How many Dish TVs" ng-model="connectTVs" ng-change="onChangeMethod()" class="selector" aria-required="true" ng-options="noOfTV as noOfTV.value for noOfTV in noOfTVs">
</select>
</div>
</div>

关于javascript - ng-selected 不更新下拉列表 angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49067823/

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