forked from refactorthis/AngularJS-Color-Picker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (44 loc) · 1.39 KB
/
app.js
File metadata and controls
48 lines (44 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var app = angular.module('myApp', []);
app.directive('colorPicker', function() {
return {
scope: {
color: '=colorPicker'
},
link: function(scope, element, attrs) {
element.colorPicker({
// initialize the color to the color on the scope
pickerDefault: scope.color,
// update the scope whenever we pick a new color
onColorChange: function(id, newValue) {
scope.$apply(function() {
scope.color = newValue;
});
// there is a currently a bug in Angular where you have to
// call $apply() twice if you're setting an attribute on
// an isolate scope property bound with =
// (in our case, '=colorPicker')
//
// See https://github.com/angular/angular.js/issues/1276
scope.$apply();
}
});
// update the color picker whenever the value on the scope changes
scope.$watch('color', function(value) {
element.val(value);
element.change();
});
}
}
});
app.controller('MainController', function($scope) {
$scope.project = {
color: "#ff0000"
};
$scope.randomColor = function() {
var red = Math.floor(Math.random() * 255);
var green = Math.floor(Math.random() * 255);
var blue = Math.floor(Math.random() * 255);
return "rgb(" + red + "," + green + "," + blue + ")";
};
});