gpt4 book ai didi

javascript - NgSelect 未在选择中选择选项

转载 作者:行者123 更新时间:2023-11-30 20:13:28 25 4
gpt4 key购买 nike

即使比较正确,Ng-Selected 也没有选择正确的选项。我可以在输出中看到比较工作正常。

我的实际问题是,即使比较成功(与 Int 和字符串比较返回 false 的其他问题不同)。为什么不选择该选项。

我不能将选项用于其他目的。其次,在复制到代码段时出现了一些错误。这就是它不起作用的原因。

var app = angular.module("app", []);
app.controller("HelloController", function($scope) {

$scope.data = {
ExpertiseId: null,
userExperties = [{
id: 1,
ExpertyTitle: "Human Resource"
}, {
id: 2,
ExpertyTitle: "Account & Finance"
}, {
id: 3,
ExpertyTitle: "Information Technology"
}, {
id: 4,
ExpertyTitle: "Business Management"
}];
}
});
<!DOCTYPE html>
<html lang="en">

<head>
<title>AngularJS</title>

</head>

<body ng-app="app">
<select name="ChooseExpertise" id="ChooseExpertise" class="form-control" ng-model="newAdmin.ExpertiseId" required>
<option style="display:none" value="">CHOOSE_EXPERTISE</option>
<option ng-selected="{{option.id.toString() == data.ExpertiseId.toString()}}" value="{{option.id.toString()}}" ng-repeat="option in data.userExperties">{{option.id.toString()==data.ExpertiseId.toString()}}---{{option.ExpertyTitle}}</option>
</select>
</body>

</html>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

谢谢 :) 请帮忙

最佳答案

您的代码中有几个错误。

  1. 对象符号错误
  2. 而且你没用过 ng-controller
  3. 将脚本文件放在 html 标签之外。
  4. 另外 ng-selected 语法错误

我更正了它们并更新了代码段。

var app = angular.module("app", []);
app.controller("HelloController", function($scope) {

$scope.data = {
ExpertiseId: 2,
userExperties : [{
id: 1,
ExpertyTitle: "Human Resource"
}, {
id: 2,
ExpertyTitle: "Account & Finance"
}, {
id: 3,
ExpertyTitle: "Information Technology"
}, {
id: 4,
ExpertyTitle: "Business Management"
}]
}
});
<!DOCTYPE html>
<html lang="en">

<head>
<title>AngularJS</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>
</head>

<body ng-app="app" >
<div ng-controller="HelloController">
<select name="ChooseExpertise" id="ChooseExpertise" class="form-control" ng-model="newAdmin.ExpertiseId" required>
<option ng-selected="option.id === data.ExpertiseId" ng-repeat="option in data.userExperties" value="{{option.id}}">{{option.id}}</option>
</select>
</div>
</body>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</html>

关于javascript - NgSelect 未在选择中选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52166153/

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