2017-03-04 23 views
0

我有一個數組中的角色列表$scope.role = [];我需要顯示具有前3個字符的芯片形式的選定角色。所以我用substr(0,3)$scope.multiRoles那裏我推我選擇的角色。我的問題是,當我刪除角色,我檢查索引,並推回數據返回$scope.role其中子字符串被推動,但不是原來的。例如文件推,而不是醫生通過比較substr值從數組中刪除值

$scope.role.push("Doctor","Engineer","Lawyer","Designer"); 

添加角色:

$scope.AddRole = function(index){ 
    if($scope.model.role !== undefined){ 
    $scope.multiRoles.push($scope.model.role.substr(0,3)); 
    var index = $scope.role.indexOf($scope.model.role); 
    $scope.role.splice(index,1); 
      }} 

刪除角色:

 $scope.removeRoles = function(index,data){ 
      if(($scope.multiRoles!== null) && ($scope.multiRoles.length>1)) 
      var index = $scope.multiRoles.indexOf(data); 
      $scope.multiRoles.splice(index,1); 
     $scope.role.push(data); 
         }; 

HTML:

<span class="file-tag-baloon1" alue ="data" 
    ng-repeat="role in filteredRoles track by $index" > 
    <span>{{role}}</span> 
    <a ng-click="removeRoles($index,role)"class="remove1">X</a> 
</span> 

回答

-1

的問題是,當你從$scope.role刪除角色你失去了這個角色信息,因此你無法恢復。作爲一種解決方案,您可以在兩個數組中存儲角色的完整名稱,並且僅在您的html中使用子字符串,如{{multiRole.substr(0, 3)}},因此您將擁有兩個數據可以兩種方式無損失的數據。