I use a standard HTML color picker in my application:
<input type="color" ng-model="vm.currentUser.color" />
If I click the button of that color picker and manually change the color, then the model is updated very often. Since I have a watch on vm.currentUser.color
, the corresponding method is also invoked very often, and that's problematic.
Is there any way to set the model variable only when the OK button of the color picker is clicked?
To change the color of our element based on the value of color picker we have to use onclick () event of the element and change its CSS color property as per the selected value in color picker element. This value appears as color picker’s value attribute. using javascript, we will change the color of our element as the chosen value in color picker.
The methods to reduce the number of colors in an image include: 1 Reduce Colors of Truecolor Image Using Color Approximation 2 Reduce Colors of Indexed Image Using imapprox 3 Reduce Colors Using Dithering More ...
If the input image uses fewer colors than the number you specify, the output colormap will have fewer than n colors, and the output image will contain all of the colors of the input image. The following figure shows the same two-dimensional slice of the color cube as shown in the preceding figure (demonstrating uniform quantization).
In general, you should limit indexed images to 256 colors for the following reasons: On systems with 8-bit display, indexed images with more than 256 colors will need to be dithered or mapped and, therefore, might not display well.
You can use ng-change
on that field as it triggers only after the color popup is closed and if a change occurs (no matter how many times you change the color inside the popup): http://plnkr.co/edit/AjDgoaUFky20vNCfu04O?p=preview
angular.module('app', [])
.controller('Ctrl', function($scope, $timeout) {
$scope.x = '#ff0000';
$scope.res = '';
$scope.a = function(x) {
console.log(x);
$scope.res = 'Color changed to ' + x;
$timeout(function(){$scope.res = ''}, 2000);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Ctrl">
<input type="color" ng-model="x" ng-change="a(x)">
<p>{{res}}</p>
</div>
Since AngularJS 1.3.0-beta.6, there is a ngModelOptions
directive, perfectly suited to your problem. It allows you to "tune how model updates are done", which is perfectly what you're looking for.
For instance, you can update the model only if the value doesn't change after a reasonable amount of time. I'll use a value of 500 milliseconds here in order to make the effect really apparent, but in practice, a lower one seems more appropriate.
<input
type="color"
ng-model="color"
ng-model-options="{debounce: 500}"
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With