1

我正在尝试$watch在发布链接功能中应用属性。

我的代码是这样的:

compile: function(){
     return {
         post:function(scope,element){
             scope.$watch('scope.x', function(){
                console.log(scope.x);
            })  
         }
     }
}

它只在页面加载时执行一次。有什么我无法弄清楚的问题吗?

编辑 :

我正在使用 angular gridster 在网格中显示各种图表。我的 index.html 看起来像:

<div gridster>
    <ul>
        <chart ng-repeat="chart in charts">
        </chart>
   </ul>
</div>

chart 是另一个带有视图的指令:

<li gridster-item row="grid_row" col="grid_col">

脚本文件包含:

module.directive('chart', ['',
    function() {
        return {
            restrict: 'E',
            scope: {
                  grid_col: '=col',
                  grid_row: '=row',
            },
            compile: function(element, attrs) {
                return {
                     post: function(scope, element, timeout) {
                         scope.$watch('grid_col', function() {
                         console.log(scope.grid_col);
                       })
                    }
                }
            }
        };
    }
]);
4

1 回答 1

0

You shouldn't mention scope name in string provided to $watch.

scope.$watch('x', function(){
    console.log(scope.x);
})

$watch over scope will place inside $$watchers object inside $scope & evaluats each string value against scope on each digest loop.

于 2016-04-15T13:09:58.907 回答