当在模型中找不到匹配项时,我如何告诉angular为下拉菜单设置默认值



我有以下选择:

<select class="form-control" id="selSigningAuthorityDisplayName"
        ng-model="selectedUser.SigningAuthority"
        ng-options="signingAuthority as signingAuthority.SigningAuthorityDisplayName for signingAuthority in signingAuthorityList track by signingAuthority.SigningAuthoritySystemName">
</select>

当我编辑现有的用户配置文件并且填充了selectedUser.SigningAuthority对象时,这非常有效。下拉框默认为预期的SigningAuthority值。但是,当我尝试添加新的用户配置文件,并且selectedUser.SigingingAuthoritynull时,我希望下拉列表默认为特定值。当selectedUser.SigningAuthority为空时,如何告诉angular选择特定的默认值?

添加默认选项选择标签,与ng-model=null 相同

<select class="form-control" id="selSigningAuthorityDisplayName" ng-model="selectedUser.SigningAuthority" ng-options="signingAuthority as signingAuthority.SigningAuthorityDisplayName for signingAuthority in signingAuthorityList track by signingAuthority.SigningAuthoritySystemName">
     <option value="">Not selected</option>
</select>

也可以在控制器内部为ng模型赋值;

$scope.selectedUser = {};
$scope.selectedUser.SigningAuthority = $scope.selectedUser.SigningAuthority || $scope.signingAuthorityList[0]; 
//or whatever index; But note, your specific value must be inside this list

最新更新