angularjs – 如何$watch多个变量的变化

前端之家收集整理的这篇文章主要介绍了angularjs – 如何$watch多个变量的变化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Watch multiple $scope attributes10个答案如何$ scope。$在Angular中观察多个变量,并且当其中一个更改时触发回调。
  1. $scope.name = ...
  2. $scope.age = ...
  3. $scope.$watch('???',function(){
  4. //called when name or age changed
  5. })
更新

Angular现在提供了两个范围方法$watchGroup(自1.3版本)和$watchCollection.这些被@blazemonger和@kargold提及。

这应该独立于类型和值:

  1. $scope.$watch('[age,name]',function () { ... },true);

在这种情况下,必须将第三个参数设置为true。

字符串连接’年龄名称’将失败在这样的情况下:

  1. <button ng-init="age=42;name='foo'" ng-click="age=4;name='2foo'">click</button>

用户单击按钮之前,观看的值将是42foo(42 foo),然后单击42foo(4 2foo)。所以watch函数不会被调用。所以更好地使用数组表达式,如果你不能确保,这样的情况不会出现。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <Meta charset="UTF-8">
  5. <link href="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css" rel="stylesheet" />
  6. <script src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
  7. <script src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
  8. <script src="http://code.angularjs.org/1.2.0-rc.2/angular.js"></script>
  9. <script src="http://code.angularjs.org/1.2.0-rc.2/angular-mocks.js"></script>
  10. <script>
  11.  
  12. angular.module('demo',[]).controller('MainCtrl',function ($scope) {
  13.  
  14. $scope.firstWatchFunctionCounter = 0;
  15. $scope.secondWatchFunctionCounter = 0;
  16.  
  17. $scope.$watch('[age,function () { $scope.firstWatchFunctionCounter++; },true);
  18. $scope.$watch('age + name',function () { $scope.secondWatchFunctionCounter++; });
  19. });
  20.  
  21. describe('Demo module',function () {
  22. beforeEach(module('demo'));
  23. describe('MainCtrl',function () {
  24. it('watch function should increment a counter',inject(function ($controller,$rootScope) {
  25. var scope = $rootScope.$new();
  26. scope.age = 42;
  27. scope.name = 'foo';
  28. var ctrl = $controller('MainCtrl',{ '$scope': scope });
  29. scope.$digest();
  30.  
  31. expect(scope.firstWatchFunctionCounter).toBe(1);
  32. expect(scope.secondWatchFunctionCounter).toBe(1);
  33.  
  34. scope.age = 4;
  35. scope.name = '2foo';
  36. scope.$digest();
  37.  
  38. expect(scope.firstWatchFunctionCounter).toBe(2);
  39. expect(scope.secondWatchFunctionCounter).toBe(2); // This will fail!
  40. }));
  41. });
  42. });
  43.  
  44.  
  45. (function () {
  46. var jasmineEnv = jasmine.getEnv();
  47. var htmlReporter = new jasmine.HtmlReporter();
  48. jasmineEnv.addReporter(htmlReporter);
  49. jasmineEnv.specFilter = function (spec) {
  50. return htmlReporter.specFilter(spec);
  51. };
  52. var currentWindowOnload = window.onload;
  53. window.onload = function() {
  54. if (currentWindowOnload) {
  55. currentWindowOnload();
  56. }
  57. execJasmine();
  58. };
  59. function execJasmine() {
  60. jasmineEnv.execute();
  61. }
  62. })();
  63.  
  64. </script>
  65. </head>
  66. <body></body>
  67. </html>

http://plnkr.co/edit/2DwCOftQTltWFbEDiDlA?p=preview

PS:

如@reblace在注释中所述,当然可以访问这些值:

  1. $scope.$watch('[age,function (newValue,oldValue) {
  2. var newAge = newValue[0];
  3. var newName = newValue[1];
  4. var oldAge = oldValue[0];
  5. var oldName = oldValue[1];
  6. },true);

猜你在找的Angularjs相关文章