通过比较substr值

时间:2017-03-04 12:23:26

标签: angularjs

我有数组$scope.role = [];中的角色列表我需要以前3个字符的筹码形式显示所选角色。所以我在substr(0,3)中使用了$scope.multiRoles我正在推动我选择的角色。我的问题是当我删除角色时,我正在检查索引并将数据推回$scope.role子字符串被推送但不是原始子字符串。例如,Doc被推送而不是Doctor

  $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 个答案:

答案 0 :(得分:-1)

问题是,当您从$scope.role删除角色时,您将丢失有关此角色的信息,因此您无法恢复该角色。作为一种解决方案,您可以在两个数组中存储角色的全名,并使用子字符串仅在您的html中表示{{multiRole.substr(0, 3)}},因此您将拥有两个数据,这些数据可以双向无任何损失。