1

HTML:

<br/><button class="btn btn-primary" ng-disabled="enableCompare()" ng-click="on_compare_plateformes($item)">Comparer</button>

JS:

propertiesModule.controller('PropertiesComparePlateformesCtrl', ['$scope', 'compareService', '$routeParams', 'PropertiesService', 'ApplicationService', 'PlatformService', 'Page', function ($scope, compareService, $routeParams, PropertiesService, ApplicationService, PlatformService, Page) {

     $scope.on_compare_plateformes = function () {
    ...
    };
..
}]);

propertiesModule.directive('propertiesListCompare', ['compareService', function (compareService) {
    return {
        restrict: 'E',
        scope: {
            properties: '=',
            propertiescible: '=',
            application: '=',
            applicationcible: '=',
            undo: '=',
            myselectref: '=',
            myselectcible: '='
        },
        templateUrl: "properties/properties-list-compare.html",
        link: function (scope, element, attrs) {
            // scope.$watch("propertiescible", function () {
            var unregister = scope.$watch('[properties, propertiescible]',function () {
                  ...
              }, true);
                 ...
           unregister();
        }

    };
}]);

当我点击我的按钮时如何重新绑定 $watcher .?

4

2 回答 2

0

我猜,通过调用unregister函数你想解除监听器的绑定。但是你应该在范围被销毁时这样做,而不是在观察者被设置之后。所以,

link: function (scope, element, attrs) {
  var unregister = scope.$watch('[properties, propertiescible]', function () {
  // do stuff
  }, true);
  scope.$on('$destroy', unregister);
}
于 2014-11-27T15:44:13.783 回答
0

要在解除绑定后再次绑定观察者,您需要再次调用 $watch,除此之外没有 Angularjs 方法可以再次绑定。为此,您可以创建一个函数:

function watchProps(){
    return scope.$watch('[properties, propertiescible]', function () {
              ..
           }, true);
    }

然后将返回的注销函数保存到变量中。

var deWatchProps = watchProps();

要再次观看,只需致电

var deWatchProps = watchProps();

您还可以像这样创建切换功能:

function toggleWatchProps(){
        if(scope.watchProps)
        {
            scope.watchProps();
        }
       else{
           scope.watchProps = scope.$watch('[properties, propertiescible]', function () {
              ..
           }, true);
        }
    }
于 2015-12-05T08:15:21.967 回答