Ng 在 select 中的 Option 中选择不 select
NgSelect not selecting the Option in select
即使比较正确,Ng-Selected 也没有选择正确的选项。我可以在输出中看到比较工作正常。
我的实际问题是,即使在比较成功之后(与其他 Int 和字符串比较 returns 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>
谢谢 :) 请帮助
您的代码中有几个错误。
- 对象表示法错误
- 而且你没用过 ng-controller
- 将脚本文件放在 html 标签之外。
- 另外 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>
即使比较正确,Ng-Selected 也没有选择正确的选项。我可以在输出中看到比较工作正常。
我的实际问题是,即使在比较成功之后(与其他 Int 和字符串比较 returns 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>
谢谢 :) 请帮助
您的代码中有几个错误。
- 对象表示法错误
- 而且你没用过 ng-controller
- 将脚本文件放在 html 标签之外。
- 另外 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>