2015-12-15 92 views
3

雖然我已經有了這個工作,但它的工作方式似乎不合適。我有一個顯示團隊列表的下拉列表(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> 

有沒有更好的方式來做到這一點?

+0

我想指出,儘管看起來像一個「黑客」相比,它周圍的東西,你也不會找到一個更快/更清潔的方式(除非可能已經擁有手頭選擇的原生js對象,但ById很快,而不是太多)。我會建議將這個魔術字符串移動到某個地方,以方便更改,或者從庫中動態傳遞(應該知道),但純粹的angularjs解決方案可能不是我可以幫到的。 G'luck! – abluejelly

回答

2

您可以隨時刪除用戶選擇佔位符選項的權限,對嗎?這樣的事情:

<option value="" disabled selected hidden>Select Team...</option> 
3

你的html部分看起來不錯,但我認爲在js方面你做了很多邏輯。如果在服務器上添加新選項會發生什麼情況?最好從後端獲取新用戶的狀態,使用select和其他小部件對其進行自定義,並在提交之前將其保留。在僞代碼它看起來像

$scope.addUser = function() { 
    //create empty user on the scope 
    $scope.selectedUser = {}; 
    //get the new user state from the backend 
    UserService.resetToNewUser($scope.selectedUser); 
    //setup view options 
    $scope.accordionStatus = {isFirstOpen: true, isFirstDisabled: false} 
}; 

app.service('UserService', function(){ 
    this.resetToNewUser = function(user){ 
     $http({ 
      method: 'GET', 
      url: '/default_user/' 
     }).then(function successCallback(response) { 
      user = response; 
     ); 
    };   
});