简体   繁体   English

角$ watch不触发

[英]Angular $watch not triggering

We have one controller and one directive. 我们有一个控制器和一个指令。 In the controller's $scope there is modules array that contains some objects. 在控制器的$ scope中,有一个包含一些对象的modules数组。

$scope.modules = [
   {title: 'demo'},
   {title: 'demo 2'},
   {title: 'demo 3'},
];

I am creating directive like this: 我正在创建这样的指令:

var popup = angular.element('<module-popup module="module"></module-popup>');
$compile(popup)($scope);

Listening to controller's $scope change: 监听控制器的$ scope更改:

$scope.$watch('title', function() {
    console.log('Controller changed');
}, true);

When page loads I see in console: Controller changed 页面加载后,我在控制台中看到: Controller changed

But when I am trying to change these values manually - in console the are no records BUT at the screen i see correct values. 但是,当我尝试手动更改这些值时-在控制台中,没有记录但在屏幕上却看到正确的值。

<h2>{{ module.title }}</h2>

$scope.$apply() throws an error $scope.$apply()引发错误

What am I doing wrong? 我究竟做错了什么? Thanks 谢谢

you're watching title but that isn't in your scope, the scope your trying to watch is modules.title or simply modules 您正在观看标题,但不在您的范围内,您尝试观看的范围是modules.title或仅仅是modules

  $scope.$watch('modules', function() {
        console.log('Controller changed');
    }, true);

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

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