0

I have an array $scope.multiRoles , I need to remove the values inside it by clicking remove button. And the removed value should moved to another array $scope.role. I am able to remove the array by calling removeRole() but couldn't move the removed values into another array. Need assistance.

Html:

<div ng-if="rolesAdded" class = "col-sm-12 col-xs-12">
  <span class="tag label tagStyle newStyling" value ="data" ng-repeat="data in multiRoles track by $index">
    <span>{{data}}</span>
    <a><i ng-click="removeRoles(index)"class="remove glyphicon glyphicon-remove-sign "></i></a> 
  </span>
</div>

JS:

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

  $scope.rolesAdded = true;
};
1
  • Duplicate here? Commented Oct 7, 2016 at 10:05

4 Answers 4

1
$scope.role = [];
$scope.removeRoles = function (index) {
if (($scope.multiRoles !== null) && ($scope.multiRoles.length > 1)) {
    $scope.role.push($scope.multiRoles[index])
    $scope.multiRoles.splice(index, 1);
}
$scope.rolesAdded = true;
};
Sign up to request clarification or add additional context in comments.

3 Comments

You will have just push that element to the another array before remove it, that's it :)
Good job, but it's always nice if you could include more explanation of your code.
Yes yes that's right, but i wrote it from existing question's code. But your suggestion is actually good idea, Thanks. :)
1

You are not adding it anywhere. And since you are using the index for your logic you need to add it in the other array before removing it. Consider this

$scope.removeRoles = function(index){
    if($scope.multiRoles !== null && $scope.multiRoles.length > 1) {
        $scope.role.push($scope.multiRoles[index]);
        $scope.multiRoles.splice(index,1);            
    }
    $scope.rolesAdded = true;
};

Also $scope.role should be an existing array

Comments

0

I think pushing the value before splicing the array can work.

Try this:

 $scope.removeRoles = function(index){
            if(($scope.multiRoles!== null ) && ($scope.multiRoles.length>1)) {
          $scope.role.push($scope.multiRoles[index])
          $scope.multiRoles.splice(index,1);            

            }

            $scope.rolesAdded = true;
        };

Comments

0

I got a solution :

JS :

  $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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.