雖然我已經有了這個工作,但它的工作方式似乎不合適。我有一個顯示團隊列表的下拉列表(DDL)。頂部和默認條目是「選擇團隊...」。儘管我的DDL與模型綁定,但「Select Team ...」不應該成爲它的一部分,因爲「Select Team ...」對域模型沒有意義。用angularjs恢復默認的下拉值
當用戶單擊「添加新」時,表單將清除,所有DDL應恢復爲其默認值。
下面是相關的控制器功能:
scope.addUser = function() {
resetToNewUser();
$scope.profileVisible = true;
$scope.oneAtATime = true;
$scope.accordionStatus = { isFirstOpen: true, isFirstDisabled: false };
}
function resetToNewUser() {
$scope.selectedUser.NtId = "";
$scope.selectedUser.UserId = -1;
$scope.selectedUser.IsActive = true;
$scope.selectedUser.FirstName = "";
$scope.selectedUser.LastName = "";
$scope.selectedUser.JobTitle = "";
$scope.selectedUser.Email = "";
$scope.selectedUser.SecondaryEmail = "";
$scope.selectedUser.PhoneNumber = "";
for(var i = 0; i < $scope.roleList.length; i++) {
if($scope.roleList[i].RoleSystemName.trim() === "BLU") {
$scope.selectedUser.Role = $scope.roleList[i];
}
}
$scope.selectedUser.SupervisorId = null;
//HACK BELOW//
document.getElementById('selTeam').selectedIndex = 0; // <-- This works, but feels like a hack.
$scope.selectedUser.IsRep = false;
for(var i = 0; i < $scope.signingAuthorityList.length; i++) {
if($scope.signingAuthorityList[i].SigningAuthoritySystemName === "SME") {
$scope.selectedUser.SigningAuthority = $scope.signingAuthorityList[i];
}
}
$scope.selectedUser.IsOutOfOfficeEnabled = false;
$scope.selectedUser.OutOfOfficeStartDate = null;
$scope.selectedUser.OutOfOfficeEndDate = null;
$scope.selectedUser.OutOfOfficeAppointedRepId = null;
}
這裏的DDL是如何在模板中定義:
<div class="form-group">
<label for="" class="control-label col-sm-2 required">Team</label>
<div class="col-sm-10">
<select class="form-control" id="selTeam"
ng-model="selectedUser.Team"
ng-options="team as team.TeamName for team in teamList track by team.TeamId">
<option value="">Select Team...</option>
</select>
</div>
</div>
有沒有更好的方式來做到這一點?
我想指出,儘管看起來像一個「黑客」相比,它周圍的東西,你也不會找到一個更快/更清潔的方式(除非可能已經擁有手頭選擇的原生js對象,但ById很快,而不是太多)。我會建議將這個魔術字符串移動到某個地方,以方便更改,或者從庫中動態傳遞(應該知道),但純粹的angularjs解決方案可能不是我可以幫到的。 G'luck! – abluejelly