本文介绍了$watch ngModel 从内部指令使用隔离范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从链接函数内部观察我的模型值.

I am trying to watch my model value from inside my linking function.

scope.$watch(attrs.ngModel, function() {
       console.log("Changed"); 
    });

当我在控制器中更改模型值时,不会触发 $watch 函数.

When I change the model value inside my controller, the $watch function is not triggered.

$scope.myModel = "ACT";

$timeout(function() {
   $scope.myModel = "TOTALS"; 
}, 2000);

小提琴:http://jsfiddle.net/dkrotts/BtrZH/4/

我在这里遗漏了什么?

推荐答案

问题是你$watching attrs.ngModel 等于myModel".您的范围内没有绑定myModel".你想$watch模型".这就是您的指令范围内的约束.请参阅 http://jsfiddle.net/BtrZH/5/

The problem is that you $watching attrs.ngModel which is equal to "myModel". You do not have "myModel" bound in your scope. You want to $watch "model". That is what is bound in the scope of your directive. See http://jsfiddle.net/BtrZH/5/

这篇关于$watch ngModel 从内部指令使用隔离范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:42