var addnew = angular.module('tailorapp',[]);
addnew.controller('mainController', function($scope,$rootScope) {
console.log('dd');
$scope.colors = [{
c: 'green'
}, {
c: '#F8F8F8'
}, {
c: 'rgb(50, 77, 32)'
}];
});

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
</head>
<body ng-app="tailorapp" ng-controller="mainController">
<div style="height:100px;width:300px;border:1px red solid; background:red" ng-style="{'background-color': {{selectedcolor.scr}} }" ng-model="divison">
</div>
<div ng-repeat="color in colors" style="width:100px; height:100px; display:inline-block" ng-style="{'background-color': color.c}" ng-click="selectedcolor.scr=black">
</div>
</body>
</html>
&#13;
我已经尝试过这个片段。我想要的是从3种颜色下面点击任何颜色,上面div的背景颜色应该相应地改变。
帮助我在哪里犯错误。
答案 0 :(得分:2)
您需要初始化selectedcolor
对象
var addnew = angular.module('tailorapp', []);
addnew.controller('mainController', function($scope, $rootScope) {
$scope.selectedcolor = {};
$scope.colors = [{
c: 'green'
}, {
c: '#F8F8F8'
}, {
c: 'rgb(50, 77, 32)'
}];
});
&#13;
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
</head>
<body ng-app="tailorapp" ng-controller="mainController">
<div style="height:100px;width:300px;border:1px red solid; background:red" ng-style="{'background-color': selectedcolor.scr }" ng-model="divison">
</div>
<div ng-repeat="color in colors" style="width:100px; height:100px; display:inline-block" ng-style="{'background-color': color.c}" ng-click="selectedcolor.scr = color.c;">
</div>
</body>
</html>
&#13;
答案 1 :(得分:1)
首先在控制器中声明$scope.selectedcolor = {scr:"red"}
。
然后将ng-click
更改为此
ng-click="selectedcolor.scr = color.c"
演示
var addnew = angular.module('tailorapp',[]);
addnew.controller('mainController', function($scope,$rootScope) {
console.log('dd');
$scope.selectedcolor = {scr:"red"}
$scope.colors = [{
c: 'green'
}, {
c: '#F8F8F8'
}, {
c: 'rgb(50, 77, 32)'
}];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<div ng-app="tailorapp" ng-controller="mainController">
<img style="height:100px;width:300px;border:1px red solid;opacity: 0.5" src="http://singer.developer.12thdoor.com/apis/media/tenant/image/estimate/11721715563956.png" ng-style="{'background-color': selectedcolor.scr }" ng-model="divison">
<br>
<div ng-repeat="color in colors" style="width:100px; height:100px; display:inline-block" ng-style="{'background-color': color.c}" ng-click="selectedcolor.scr = color.c">
</div>
</div>
答案 2 :(得分:1)
您需要在范围中声明selectedcolor对象。只需将它添加到您的范围对象 -
addnew.controller('mainController', function($scope,$rootScope) {
console.log('dd');
$scope.selectedcolor = ""
$scope.colors = [{
c: 'green'
}, {
c: '#F8F8F8'
}, {
c: 'rgb(50, 77, 32)'
}];
});
答案 3 :(得分:0)
以下是您的代码的工作版本。
<html ng-app="tailorapp">
<head>
<meta charset="utf-8">
<title>ng app</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js" type="text/javascript"></script>
<script>
var addnew = angular.module('tailorapp',[]);
addnew.controller('mainController', function($scope) {
$scope.colors = [{
c: 'green'
}, {
c: '#F8F8F8'
}, {
c: 'rgb(50, 77, 32)'
}];
$scope.setColor = function(value){
$scope.selectedcolor = value;
}
});
</script>
</head>
<body ng-app="tailorapp" ng-controller="mainController">
<div style="height:100px;width:300px;border:1px red solid; background:red" ng-style="{'background-color': selectedcolor }" ng-model="divison">
</div>
<div ng-repeat="color in colors" style="width:100px; height:100px; display:inline-block" ng-style="{'background-color': color.c}" ng-click="setColor(color.c)">
{{color.c}}
</div>
</body>
</html>