How to use angularJS ng-click and ng-style

Asked 2 years ago, Updated 2 years ago, 55 views

I have a question about angularJS.
I would like to use ng-repeart to create a program that changes the background color when I click on an item from the displayed list.This time I want to run the following program using the data binding function of angularJS, but it doesn't work. The list looped with ng-repeart contains multiple keys and values.This time, I would like to have a key called style with a CSS as one of the keys.

HTML File Side

<div ng-repeart="item in list"ng-click="change(item)"ng-style="{{item.style}}">
  <P> Item name to display </p>
</div>

JS File Side

$scope.change=function(check){
  if(check.style==null){
    check.style="{backgroundColor:'red'}";
  } else {
    check.style="{backgroundColor:'blue'}";
  }
}

Currently, the data is bound and the code is changed, but the way it looks remains the same.
How do I deal with this situation?

angularjs

2022-09-30 13:52

1 Answers

$scope.$apply(function(){
  if(check.style==null){
    check.style="{backgroundColor:'red'}";
  } else {
    check.style="{backgroundColor:'blue'}";
  }
});

Will this not work?


2022-09-30 13:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.