微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angularjs – 使用颜色选择器减少模型更新的数量

我在我的应用程序中使用标准的 HTML颜色选择器:

<input type="color" ng-model="vm.currentUser.color" />

如果我单击该颜色选择器的按钮并手动更改颜色,则会经常更新模型.由于我对vm.currentUser.color有一个监视,因此也经常调用相应的方法,这是有问题的.

有没有办法只在点击颜色选择器的OK按钮时设置模型变量?

Image of the color picker

解决方法

您可以在该字段上使用ng-change,因为它仅在关闭颜色弹出窗口后触发并且如果发生更改(无论您更改弹出窗口内的颜色多少次): 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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐