Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>test</title>
- </head>
- <body>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js">
- </script>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-resource.min.js">
- </script>
- <script>
- var app = angular.module("app", []);
- var w = [];
- app.controller('UnWatch', ["$scope", function ($scope) {
- $scope.watched = [{
- id: 1,
- changed: false,
- value: "this is the value",
- second: 123,
- second1: 123,
- second2: 123,
- second3: 123,
- second4: 123
- }, {
- id: 2,
- changed: false,
- value: "blabla ssd",
- second: 12356,
- second1: 123,
- second2: 123,
- second3: 123,
- second4: 123
- }];
- for (n = 3; n < 3000; n++) {
- $scope.watched.push( {
- id: n,
- changed: false,
- value: "this is the value",
- second: 123,
- second1: 123,
- second2: 123,
- second3: 123,
- second4: 123
- });
- }
- console.log($scope.$$watchers);
- console.log($scope.watched.$$watchers);
- $scope.ww = function () {
- console.log('click');
- if ($scope.$$watchers.length == 0)
- $scope.$$watchers = w;
- else {
- w = $scope.$$watchers;
- $scope.$$watchers = [];
- }
- console.log($scope.$$watchers);
- console.log($scope.watched.$$watchers);
- }
- }]).directive('smartObjectWatch', function() {
- return {
- scope: {
- smartObjectWatch: "=",
- },
- link: function link(scope, element, attrs) {
- var exp = "smartObjectWatch."+attrs.smartObjectWatchProperty;
- var w = [];
- // remove all watchers
- if (w.length == 0)
- angular.forEach(element.data().$scope.$$watchers, function(watcher, index) {
- w.push(watcher);
- });
- // remove watchers
- //element.data().$scope.$$watchers.splice(0);
- scope.$watch(exp, function(value, oldValue) {
- if (value) {
- console.log("add watchers!" + scope.smartObjectWatch.id);
- angular.forEach(w, function(watcher, index) {
- element.data().$scope.$$watchers.push(watcher);
- });
- } else {
- console.log("remove watchers!" + scope.smartObjectWatch.id);
- // store initial watchers
- if (w.length == 0)
- angular.forEach(element.data().$scope.$$watchers, function(watcher, index) {
- w.push(watcher);
- });
- // remove watchers
- element.data().$scope.$$watchers.splice(0);
- }
- });
- }
- }
- });
- </script>
- <div ng-app="app">
- <div ng-controller="UnWatch">
- <table>
- <tr ng-repeat="w in watched track by w.id" smart-object-watch="w" smart-object-watch-property="changed">
- <td>{{ w.changed }}</td><td>{{ w.value }}</td>
- <td>{{ w.second }}</td>
- <td>{{ w.second1 }}</td>
- <td>{{ w.second2 }}</td>
- <td>{{ w.second3 }}</td>
- <td>{{ w.second4 }}</td>
- <td><input ng-model="w.value"></input></td><td><a href="#" ng-click="w.changed=!w.changed">updated</td>
- </tr>
- </table>
- <a ng-click="ww()" href="#">click</a>
- <div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement