2

I was trying to code a background color changer for my angular project. This is the html code :

<h1>Background color changer</h1>

<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js"></script>
        <script>
            var app=angular.module("Myapp",[])
            app.controller("Mycontroller", function($scope)
            {
                $scope.colors=[
                    {name: 'Red',color:'red'},
                    {name: 'Yellow',color:'yellow'},
                    {name: 'Pink',color:'pink'},
                    {name: 'Cyan',color:'cyan'},
                    {name: 'Orange',color:'orange'}
                ]
            });
        </script>
    </head>
    <body>
        
        <div ng-app="Myapp" ng-controller="Mycontroller">
            <select>
                <option value="" selected hidden>Select Color</option>
                <option ng-repeat="colorselect in colors" 
                style="background-color:{colorselect.color}">(colorselect.name)</option>
                    
                
            </select>
            <p>
                <a routerLink="/loggedinsettings">Go back</a>
            </p>
            
            <hr/>
            
        </div>
    </body>
</html>

It is supposed to let you choose a color and then the color will be applied as a background. For now, it looks like this: picture

Any ideas ? Thanks !

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338

1 Answers1

0

You were close. Just need to use interpolation for retrieving the values from scope.

var app = angular.module("Myapp", [])
app.controller("Mycontroller", function($scope) {
  $scope.colors = [{
      name: 'Red',
      color: 'red'
    },
    {
      name: 'Yellow',
      color: 'yellow'
    },
    {
      name: 'Pink',
      color: 'pink'
    },
    {
      name: 'Cyan',
      color: 'cyan'
    },
    {
      name: 'Orange',
      color: 'orange'
    }
  ]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js"></script>
<h1>Background color changer</h1>
<div ng-app="Myapp" ng-controller="Mycontroller">
  <select>
    <option value="" selected hidden>Select Color</option>
    <option ng-repeat="colorselect in colors" style="background-color:{{colorselect.color}}">{{colorselect.name}}</option>


  </select>
  <p>
    <a routerLink="/loggedinsettings">Go back</a>
  </p>

  <hr/>

</div>
derloopkat
  • 6,232
  • 16
  • 38
  • 45