gpt4 book ai didi

javascript - Angular ng-options 删除空白选项并仅选择第一个选项

转载 作者:搜寻专家 更新时间:2023-10-31 22:02:29 24 4
gpt4 key购买 nike

我正在使用 AngularJS 从我的 Controller 中的对象数组动态填充我的选择选项内容。我的对象有一个名为 userProfileName 的属性。

如何删除顶部的空白选项?

此外,我想默认选择第一个配置文件 Profile 1。如何做到这一点?

我的代码片段在这里:

<select id="requestorSite" 
ng-model="selectedUserProfile"
ng-options="userProfile.userProfileName for userProfile in userProfiles"
ng-selected=""
class="form-control displayInlineBlock width40per marginLeft15">
</select>

我的 Controller 有

$scope.userProfiles

作为对象数组,每个对象都有userProfileName属性。

下面是截图: enter image description here

我想删除顶部的空白选项,并且默认选择 Profile 1

谢谢,安 git

最佳答案

这样做:)

在你的 Controller 中:

 function myCtrl ($scope) {
$scope.userProfiles = [
{id: 10, name: 'Carton'},
{id: 27, name: 'Bernard'},
{id: 39, name: 'Julie'},
];

$scope.selectedUserProfile= $scope.userProfiles[0]; // Set by default the value "carton"
};

在您的页面中:

      <select  id="requestorSite" ng-model="selectedUserProfile" ng-options="userProfile as userProfile.name for userProfile in userProfiles">
</select>

代码笔:http://codepen.io/anon/pen/qdOGVB

关于javascript - Angular ng-options 删除空白选项并仅选择第一个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30104565/

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