Javascript 角度ng选项删除空白选项并仅选择第一个选项
我使用AngularJS从控制器中的对象数组动态填充我的选择选项内容。我的对象有一个名为userProfileName的属性 如何删除我在顶部得到的空白选项? 此外,我希望在默认情况下选择第一个配置文件配置文件1。如何做到这一点 下面是我的代码片段:Javascript 角度ng选项删除空白选项并仅选择第一个选项,javascript,angularjs,html,ng-options,Javascript,Angularjs,Html,Ng Options,我使用AngularJS从控制器中的对象数组动态填充我的选择选项内容。我的对象有一个名为userProfileName的属性 如何删除我在顶部得到的空白选项? 此外,我希望在默认情况下选择第一个配置文件配置文件1。如何做到这一点 下面是我的代码片段: <select id="requestorSite" ng-model="selectedUserProfile" ng-o
<select id="requestorSite"
ng-model="selectedUserProfile"
ng-options="userProfile.userProfileName for userProfile in userProfiles"
ng-selected=""
class="form-control displayInlineBlock width40per marginLeft15">
</select>
作为对象数组,每个对象都有userProfileName属性
以下是屏幕截图:
< P> >我想删除顶部的空白选项,默认情况下也有“强>配置文件1 <强>选择。< /P>
谢谢,
安基特这样做:)
在控制器中:
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>
代码笔:这有几种解决方案。以下几点对我最有效。只需使用第一个值初始化模型。
ng init=“myItem=items[0].name“
起作用
<select ng-if="items" ng-init="myItem = items[0].name" ng-model="myItem">
<option ng-repeat="item in items" ng-value="item.name">{{item.name}}</option>
</select>
{{item.name}
现在我正在寻找空白选项问题空白选项问题?此代码在选择中放入值,但他也设置默认值,因此空白问题将得到解决。请给我们看看你的代码。它写在上面。。。选择同样的例子。。。如果你把NG模型=“MyMemodel”,这行删除空白选项:$Simult.MyMeals= $Simult.USEPrPrices(0);你的摇滚伙伴@carton谢谢。。。这起作用了。
<select ng-if="items" ng-init="myItem = items[0].name" ng-model="myItem">
<option ng-repeat="item in items" ng-value="item.name">{{item.name}}</option>
</select>