使用分离的范围从内指令$看ngModel [英] $watch ngModel from inside directive using isolate scope

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

问题描述

我想从我的链接函数内部看我的模型值。

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

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

当我改变我的控制器内的模型值中,$手表的功能不会被触发。

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/

我是什么在这里失踪?

推荐答案

的问题是,你 $观看 ING attrs.ngModel 这等于基于myModel。你不必基于myModel在你的范围内绑定。你想 $观看模式。这就是在您的指令范围的约束。请参见 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/

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆