Angularjs 重新绑定$watcher
HTML: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
如何在我单击我的按钮时重新绑定$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);
}
}