1

I created a list using divs and I want to change only background color of div when user select one from the list.

Currently I have done this by defining two different css classes with only difference is background color, and i set this style when user selects the div.

So is their anyway to change only background color without affecting existing style using angularJS?

6
  • see this answer stackoverflow.com/questions/36186268/… Commented May 11, 2016 at 4:18
  • In this it is using style for defining background color which I am also doing currently. I am looking for changing only background color without defining new style. Commented May 11, 2016 at 4:22
  • 1
    in your directive: element.css('backgroundColor', 'red') Commented May 11, 2016 at 4:53
  • please check stackoverflow.com/questions/19397987/… Commented May 11, 2016 at 5:19
  • Use like this - ng-style="{background:colorCodeScopeVar}" Commented May 11, 2016 at 5:41

2 Answers 2

2

I have just started learning AngularJS, so this is kind of home work for me. I know this may not be the clean way or exactly what you want. See if it makes sense. If not let me know, I would love to improve.

Currently I have done this by defining two different css classes with only difference is background color

You can put background color into separate classes and toggle just that class keeping everything else same.

HTML

<div ng-app ng-controller="testCtrl" >
  <div ng-repeat="item in items">
    <div ng-class="item.class" ng-click="select(item)">{{item.text}}</div>
  </div>
</div>

JS

function testCtrl($scope) {
  $scope.items = [
    { text:'List item 1', class:'default normal' },
    { text:'List Item 2', class:'default normal' },
    { text:'List Item 3', class:'default normal' }
  ];

  $scope.select = function(item) {
    angular.forEach($scope.items, function (value, key) {
        if(value == item) {
        value.class = 'default select' // selected
      }
      else {
        value.class = 'default normal' // restored
      }
    });
  };
}

CSS

.default { font-size: 25px; padding: 10px; margin-bottom: 5px; }
.normal { background: yellow; }
.select { background: cyan; }

JSFiddle https://jsfiddle.net/po2o8f69/6/

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

2 Comments

Thanks for the solution, can we avoid looping all the element when we select one of the element, is their better way to do this? also can we avoid keep style in the array?
Honestly no idea. I have just started on it. I am afraid I can't answer those questions now.
1

I hope this will help you , adding active class on clicked item

app.controller('MainCtrl', function($scope) {
    $scope.items = [
        'Page One',
        'Page Two',
        'Page Three'
    ];

    $scope.selected = 0;
    $scope.active = function(index) {
        $scope.selected = index;
    }
});



<li ng-repeat="item in items">
            <a ng-class="{ active : $index == selected }" ng-click="active($index)">{{ item }}</a>
        </li>

fiddle

1 Comment

Problem with this, I have a default style also which is applied before changing status to active state. And in active state I want to change only background color. and rest of the part should be same as set in default style.

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.