简体   繁体   English

如何在angularjs中观看此变量

[英]How to watch this variable in angularjs

I couldn't figure out how to watch a variable that binded to this variable in angularjs. 我不知道如何在angularjs中观看绑定到此变量的变量。

Here is what I have tried. 这是我尝试过的。

in html, 在html中

<input type="text" ng-model="vm.text"/>--{{vm.text}}--
    <p>{{vm.count}} times changed</p>
    <input type="text" ng-model="text1"/>--{{text1}}--
    <p>{{count1}} times changed</p>

in app.js 在app.js中

$scope.$watch('this.text', function() {
    console.log('watch 1');
    this.count=this.count+1;
});

$scope.$watch('text1', function() {
// do something here
   console.log('watch 2');
   $scope.count1=$scope.count1+1;
});

and plunker link for the same. 和punker 链接相同。

I could watch text1 but I couldn't watch text1. 我可以看text1,但不能看text1。

Can anyone please explain me how to watch text1? 谁能解释一下如何观看text1?

thanks in advance 提前致谢

You need to first bind this context to angular $scope using angular.bind 您需要先使用angular.bind this上下文绑定到angular $scope

$scope.$watch(angular.bind(this, function () {
  return this.text;
}), function (newVal) {
  console.log('watch 1');
  this.count=this.count+1;
});

Or place a function inside watcher instead of string & that will get evaluated on each digest cycle 或者将一个函数而不是字符串&放置在观察程序中,该函数将在每个摘要循环中进行评估

$scope.$watch(function () {
   return vm.text;
},function(value){
   console.log('watch 1');
   this.count=this.count+1;
});

You need to watch vm.text 您需要观看vm.text

http://plnkr.co/edit/ctDjs0dwe50kMu61TfCW?p=preview http://plnkr.co/edit/ctDjs0dwe50kMu61TfCW?p=preview

$scope.$watch('vm.text', function() {
       console.log('watch 1');
             this.count=this.count+1;

   });

You have got some separate concerns here. 您在这里有一些其他的担忧。

You are using $scope.watch with controller as syntax. 您正在将$ scope.watch与控制器一起使用。 It is not a simple text, here is a fine text about it: 这不是一个简单的文本,这是一个很好的文本:

http://www.bennadel.com/blog/2852-understanding-how-to-use-scope-watch-with-controller-as-in-angularjs.htm http://www.bennadel.com/blog/2852-understanding-how-to-use-scope-watch-with-controller-as-in-angularjs.htm

and here is my fork of your plunker: 这是我pl下的叉子:

http://plnkr.co/edit/ctDjs0dwe50kMu61TfCW?p=preview http://plnkr.co/edit/ctDjs0dwe50kMu61TfCW?p=preview

What I did is: 我所做的是:

/*make this (controller) callable 
 *from inside my watch function, 
 *so I can observe it and count can change
 */
var vm = this; 

//vm is observable as it is assigned
$scope.$watch(function (scope) {return vm.text;}, 


function() {
   console.log('watch 1');
  //count can change, as the controller is assigned to variable
   vm.count=vm.count+1;       
});

It is a funny situation in which scope can help not only by making watch smoothly callable, but also by being easily referenced from inside watch function ($scope.watch). 这是一个很有趣的情况,在这种情况下,范围不仅可以通过使手表可平滑调用,而且还可以从手表内部函数($ scope.watch)中轻松引用来提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM