I want the program to select a color from the dropdown list and set as background color. I have written a code for it. I have used directives – ng-model, ng-style,ng-repeat.
<html ng-app="Swabhav.Color">
<head>
<title>Color-pick</title>
<script src="angular.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-
bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css"
/>
<script
src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js">
</script>
</head>
<body>
<script>
var app = angular.module("Swabhav.Color", []);
app.controller("dropdowncolor", ["$scope",function($scope)
{
$scope.colors = [{ name: "Red", hex: "#F21B1B" },
{ name: "Blue", hex: "#0099CC" },
{ name: "Green", hex: "#07BA16" },
{ name: "Yellow", hex: "#FFFF99"}];
}]);
</script>
<div ng-controller="dropdowncolor">
<p>Select a color</p>
<select ng-model="selectedcolor">
<option ng-repeat="selectcolor in colors" value="{{selectcolor.hex}}">
your selected color is : {{selectcolor.name}}
<p ng-style="{'background-color':selectcolor.hex}" ></p>
</option>
</select>
</div>
</body>
</html>
2
Answers
You can try like the below code, also check this working plunker link for your example scenario,
Templates:
Controller:
All you need to do is change your P tag like below