Angular 多个 select 未预 select 用于非空 ng 模型
Angular multiple select not preselecting for non-empty ng-model
我已将 $scope.selectedUsers
设置为仅包含 1 个对象的数组,该对象与我可用的所有 $scope.users
列表中的对象之一完全匹配,但未突出显示所选用户在我的 <select multiple...>
中。不应该突出显示吗?
Js:
$scope.selectedUsers = [
{ id: 2, name: "Jenny" }
];
$scope.users = [
{ id: 1, name: "Frank" },
{ id: 2, name: "Jenny" }
];
Html:
<select multiple ng-model="selectedUsers" ng-options="user as user.name for user in users"></select>
现场演示:http://plnkr.co/edit/wpfvhvuShFVE07cyBE6M?p=preview
which is an exact match of one of the objects from my available list of all $scope.users
不,它们不是相同的,即使两个对象具有相似的键值。 Angular 使用对象的严格比较 (===
),只有当两个对象是相同的对象(引用相同的对象)时它们才相等。
您的情况下的正确代码:
$scope.users = [
{ id: 1, name: "Frank" },
{ id: 2, name: "Jenny" }
];
$scope.selectedUsers = [
$scope.users[1]
];
我已将 $scope.selectedUsers
设置为仅包含 1 个对象的数组,该对象与我可用的所有 $scope.users
列表中的对象之一完全匹配,但未突出显示所选用户在我的 <select multiple...>
中。不应该突出显示吗?
Js:
$scope.selectedUsers = [
{ id: 2, name: "Jenny" }
];
$scope.users = [
{ id: 1, name: "Frank" },
{ id: 2, name: "Jenny" }
];
Html:
<select multiple ng-model="selectedUsers" ng-options="user as user.name for user in users"></select>
现场演示:http://plnkr.co/edit/wpfvhvuShFVE07cyBE6M?p=preview
which is an exact match of one of the objects from my available list of all $scope.users
不,它们不是相同的,即使两个对象具有相似的键值。 Angular 使用对象的严格比较 (===
),只有当两个对象是相同的对象(引用相同的对象)时它们才相等。
您的情况下的正确代码:
$scope.users = [
{ id: 1, name: "Frank" },
{ id: 2, name: "Jenny" }
];
$scope.selectedUsers = [
$scope.users[1]
];