Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce the number of model updates with a color picker

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?

Image of the color picker

like image 990
quma Avatar asked Nov 21 '15 18:11

quma


People also ask

How to change the color of an element based on color picker?

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.

How to reduce the number of colors in an image?

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 ...

How do I know if my colormap is uniform quantization?

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).

Why should I limit indexed images to 256 colors?

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.


2 Answers

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>
like image 109
Shomz Avatar answered Nov 15 '22 05:11

Shomz


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}"
/>

Example on JSFiddle

like image 36
Blackhole Avatar answered Nov 15 '22 04:11

Blackhole