Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 重新绑定$watcher_Angularjs_Angularjs Directive_Angularjs Watch - Fatal编程技术网

Angularjs 重新绑定$watcher

Angularjs 重新绑定$watcher,angularjs,angularjs-directive,angularjs-watch,Angularjs,Angularjs Directive,Angularjs Watch,HTML: 如何在我单击我的按钮时重新绑定$watcher?我想,通过调用unregister函数,您想要解除绑定侦听器。但您应该在销毁作用域时执行此操作,而不是在设置了watcher之后。所以 link: function (scope, element, attrs) { var unregister = scope.$watch('[properties, propertiescible]', function () { // do stuff }, true); scop

HTML:


如何在我单击我的按钮时重新绑定$watcher?

我想,通过调用
unregister
函数,您想要解除绑定侦听器。但您应该在销毁作用域时执行此操作,而不是在设置了watcher之后。所以

link: function (scope, element, attrs) {
  var unregister = scope.$watch('[properties, propertiescible]', function () {
  // do stuff
  }, true);
  scope.$on('$destroy', unregister);
}

要在解除绑定后再次绑定观察者,需要再次调用$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);
        }
    }
var deWatchProps = watchProps();
function toggleWatchProps(){
        if(scope.watchProps)
        {
            scope.watchProps();
        }
       else{
           scope.watchProps = scope.$watch('[properties, propertiescible]', function () {
              ..
           }, true);
        }
    }