Advertisement
Guest User

Untitled

a guest
Jul 30th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>test</title>
  4. </head>
  5.  
  6. <body>
  7. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js">
  8. </script>
  9. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-resource.min.js">
  10. </script>
  11.  
  12. <script>
  13. var app = angular.module("app", []);
  14. var w = [];
  15.  
  16. app.controller('UnWatch', ["$scope", function ($scope) {
  17. $scope.watched = [{
  18. id: 1,
  19. changed: false,
  20. value: "this is the value",
  21. second: 123,
  22. second1: 123,
  23. second2: 123,
  24. second3: 123,
  25. second4: 123
  26. }, {
  27. id: 2,
  28. changed: false,
  29. value: "blabla ssd",
  30. second: 12356,
  31. second1: 123,
  32. second2: 123,
  33. second3: 123,
  34. second4: 123
  35. }];
  36. for (n = 3; n < 3000; n++) {
  37. $scope.watched.push( {
  38. id: n,
  39. changed: false,
  40. value: "this is the value",
  41. second: 123,
  42. second1: 123,
  43. second2: 123,
  44. second3: 123,
  45. second4: 123
  46. });
  47. }
  48.  
  49. console.log($scope.$$watchers);
  50. console.log($scope.watched.$$watchers);
  51.  
  52. $scope.ww = function () {
  53. console.log('click');
  54. if ($scope.$$watchers.length == 0)
  55. $scope.$$watchers = w;
  56. else {
  57. w = $scope.$$watchers;
  58. $scope.$$watchers = [];
  59. }
  60. console.log($scope.$$watchers);
  61. console.log($scope.watched.$$watchers);
  62. }
  63. }]).directive('smartObjectWatch', function() {
  64. return {
  65. scope: {
  66. smartObjectWatch: "=",
  67. },
  68. link: function link(scope, element, attrs) {
  69. var exp = "smartObjectWatch."+attrs.smartObjectWatchProperty;
  70. var w = [];
  71. // remove all watchers
  72. if (w.length == 0)
  73. angular.forEach(element.data().$scope.$$watchers, function(watcher, index) {
  74. w.push(watcher);
  75. });
  76. // remove watchers
  77. //element.data().$scope.$$watchers.splice(0);
  78. scope.$watch(exp, function(value, oldValue) {
  79. if (value) {
  80. console.log("add watchers!" + scope.smartObjectWatch.id);
  81. angular.forEach(w, function(watcher, index) {
  82. element.data().$scope.$$watchers.push(watcher);
  83. });
  84. } else {
  85. console.log("remove watchers!" + scope.smartObjectWatch.id);
  86. // store initial watchers
  87. if (w.length == 0)
  88. angular.forEach(element.data().$scope.$$watchers, function(watcher, index) {
  89. w.push(watcher);
  90. });
  91. // remove watchers
  92. element.data().$scope.$$watchers.splice(0);
  93. }
  94. });
  95. }
  96. }
  97. });
  98.  
  99. </script>
  100. <div ng-app="app">
  101. <div ng-controller="UnWatch">
  102. <table>
  103. <tr ng-repeat="w in watched track by w.id" smart-object-watch="w" smart-object-watch-property="changed">
  104. <td>{{ w.changed }}</td><td>{{ w.value }}</td>
  105. <td>{{ w.second }}</td>
  106. <td>{{ w.second1 }}</td>
  107. <td>{{ w.second2 }}</td>
  108. <td>{{ w.second3 }}</td>
  109. <td>{{ w.second4 }}</td>
  110. <td><input ng-model="w.value"></input></td><td><a href="#" ng-click="w.changed=!w.changed">updated</td>
  111. </tr>
  112. </table>
  113. <a ng-click="ww()" href="#">click</a>
  114. <div>
  115.  
  116. </div>
  117.  
  118. </body>
  119. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement