RAHUL DEEP
RAHUL DEEP

Reputation: 655

Angularjs setting list elements background-colour according to a JSON

I am having a list which has few elements coming from JSON file. I want each element of list to have background colour which we get from a function getNumber().This function will generate a random number and then will return a colour corresponding to that random number. Now i want a solution through which i can set the background-color of each divs by calling getNumber() function.

HTML code-

<html ng-app="ionicApp">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

  <title>Radio Buttons</title>

  <link href="//code.ionicframework.com/1.0.0-beta.12/css/ionic.css" rel="stylesheet">
  <script src="//code.ionicframework.com/nightly/js/ionic.bundle.min.js"></script>
</head>

<body ng-controller="MainCtrl">

  <ion-header-bar class="bar-positive">
    <h1 class="title">Divs inside Div</h1>
  </ion-header-bar>

  <ion-content scroll="false">
    <ion-scroll class="list" direction="y" style="height:100%">
      <li class="item" ng-repeat="item in agendaDetails">
        <div class="row speakerListItems" ng-repeat="speakering in item.speakers track by $index">
          <div class="col-50">
            <p style="float:left">{{speakering.speaker}}</p>
          </div>
          <div class="col-50">
            <p style="float:right">Hello</p>
          </div>
        </div>
      </li>
    </ion-scroll>
  </ion-content>
</body> 
</html>

Here i want to give background-color to each list item by calling getNumber() and checking the colour corresponding to that value dynamically. Here is codepen Link-CODEPEN DEMO

Upvotes: 0

Views: 2473

Answers (2)

roshini
roshini

Reputation: 112

i have tried it's working fine check following code

<html ng-app="ionicApp">

<head ng-app="ionicApp">
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

    <title>Radio Buttons</title>

    <link href="//code.ionicframework.com/1.0.0-beta.12/css/ionic.css" rel="stylesheet">
    <style>
        .speakerListItems
        {
            border-bottom:1px solid white;
        }
    </style>
    <script src="//code.ionicframework.com/nightly/js/ionic.bundle.min.js"></script>
</head>

<body ng-controller="MainCtrl">

<ion-header-bar class="bar-positive">
    <h1 class="title">Divs inside Div</h1>
</ion-header-bar>

<ion-content scroll="false">
    <ion-scroll class="list" direction="y" style="height:100%">
        <li class="item" ng-repeat="item in agendaDetails">
            <div class="row speakerListItems" ng-repeat="speakering in item.speakers track by $index">
                <div class="col-50">
                    <p ng-style="{'background-color':getNumber()}">{{speakering.speaker}}</p>
                </div>
                <div class="col-50">
                    <p style="float:right">Hello</p>
                </div>
            </div>
        </li>
    </ion-scroll>
</ion-content>

<script>
            angular.module('ionicApp', ['ionic'])

                    .controller('MainCtrl', function ($scope) {
                        var agendaDetails = [
                            {
                                "startTime": "10:00",
                                "endTime": "12:00",
                                "topic": "INVESTOR RELATIONS & FINANCE OVERVIEW",
                                "speakers": [
                                    {
                                        "speaker": "Speaker1"
                                    }
                                ]
                            },
                            {
                                "startTime": "12:00",
                                "endTime": "2:00",
                                "topic": "ACQUISTION PROCESS",
                                "speakers": [
                                    {
                                        "speaker": "Speaker2"
                                    },
                                    {
                                        "speaker": "Speaker3"
                                    }
                                ]
                            },
                            {
                                "startTime": "2:00",
                                "endTime": "4:00",
                                "topic": "DIVERSITY OVERVIEW",
                                "speakers": [
                                    {
                                        "speaker": "Speaker4"
                                    },
                                    {
                                        "speaker": "Speaker5"
                                    },
                                    {
                                        "speaker": "Speaker6"
                                    },
                                ]
                            },
                            {
                                "startTime": "10:00",
                                "endTime": "12:00",
                                "topic": "INVESTOR RELATIONS & FINANCE OVERVIEW",
                                "speakers": [
                                    {
                                        "speaker": "Speaker7"
                                    },
                                    {
                                        "speaker": "Speaker8"
                                    },
                                    {
                                        "speaker": "Speaker9"
                                    },
                                    {
                                        "speaker": "Speaker10"
                                    },
                                ]
                            }
                        ];
                        $scope.agendaDetails = agendaDetails;
                         var colorDetails = {
                            "1": "red",
                            "2": "blue",
                            "3": "black",
                            "4": "white",
                            "5": "green"
                        }
                        $scope.colorDetails = colorDetails;

                        $scope.getNumber = function (){
                            var minNumber = 1;
                            var maxNumber = 4;
                            var randomnumber = Math.floor(Math.random() * (maxNumber + 1) + minNumber);                                
                            return($scope.colorDetails[randomnumber]);
                        };
                    });
</script>

ng-style is the solution ,if we are trying with style="background-color:red or getNumber() is notworking and that too when ever we trying to call a function quotes not required that is sure.

Upvotes: 0

Kulbhushan Singh
Kulbhushan Singh

Reputation: 536

Try ng-style as follows: codepen sample

<ion-content scroll="false">
    <ion-scroll class="list" direction="y" style="height:100%">
      <li class="item" ng-repeat="item in agendaDetails">
        <div class="row speakerListItems" ng-repeat="speakering in item.speakers track by $index">
          <div class="col-50" ng-style="{'background-color': getNumber()}">
            <p style="float:left">{{speakering.speaker}}</p>
          </div>
          <div class="col-50" >
            <p style="float:right">Hello</p>
          </div>
        </div>
      </li>
    </ion-scroll>
  </ion-content>

and you must put getNumber method on $scope so that it can be called from HTML

$scope.getNumber = function getNumber() {
    var minNumber = 1; 
    var maxNumber = 4; 
    var randomnumber = Math.floor(Math.random() * (maxNumber + 1) + minNumber);
  //alert("randomnumber"+randomnumber);
  //alert($scope.colorDetails[randomnumber]);
  return($scope.colorDetails[randomnumber]);
}

Upvotes: 2

Related Questions