2

I am using angularjs I have two list when I click first one I will push the value into another scope and bind the value to second list. Now my requirement is when first list values which are moved to second list, I need to change the color of moved values in list1 Here I attached my fiddle

Fiddle

3 Answers 3

1

You can use findIndex and ng-class together to check if the second list contains the same item as first. If present apply css class to the first list item.

JS:

   $scope.checkColor = function(text) {
       var index = $scope.linesTwos.findIndex(x => x.text === text);
       if (index > -1) return true;
       else return false;
   }

HTML:

<li ng-click="Team($index,line.text)" ng-class="{'change-color':checkColor(line.text)}">{{line.text}}</li>

Working Demo: https://jsfiddle.net/7MhLd/2659/

Sign up to request clarification or add additional context in comments.

Comments

1

You can do something like this:

var myApp = angular.module('myApp', []);


function MyCtrl($scope) {
  $scope.lines = [{
      text: 'res1'
    },
    {
      text: 'res2'
    },
    {
      text: 'res3'
    }
  ];

  $scope.linesTwos = [];
  $scope.Team = function(index, text) {
    var obj = {};
    obj.text = text;
    $scope.linesTwos.push(obj)
  }

  $scope.Team2 = function(index, text2) {

    $scope.linesTwos.splice(index, 1)
  }

$scope.containsObj = function(obj, list) {
    var i;
    for (i = 0; i < list.length; i++) {
        if (angular.equals(list[i], obj)) {
            return true;
        }
    }

    return false;
};
}
.clicked {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">




  <ul ng-repeat="line in lines">
    <li ng-class="{'clicked': containsObj(line,linesTwos)}" ng-click="Team($index,line.text)">{{line.text}}</li>

  </ul>
  <ul>
    <li>__________</li>

  </ul>
  <ul ng-repeat="line in linesTwos">
    <li ng-click="Team2($index,line.text)">{{line.text}}</li>

  </ul>
</div>

Comments

0

you have to achieve it using ng-class and create a dynamic class style for pushed data please check my working example fiddle JS fiddle sample

in HTML nedd to do these changes

<li ng-click="Team($index,line.text,line)" ng-class="{'pushed':line.pushed}">

<li ng-click="Team2($index,line.text,line)">

In css

.pushed{color:red;}

In Controller

`$scope.Team=function(index,text,line){
  var obj={};
  obj = line;
  $scope.linesTwos.push(obj)
  line.pushed = true;
}`

`scope.Team2 = function(index,text2,line){
  $scope.linesTwos.splice(index,1)
  line.pushed = false;
}
`

its because angular two way binding

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.