与手表的双向数据绑定

时间:2013-06-18 21:45:32

标签: angularjs angularjs-directive angularjs-scope

为什么$ scope.watch无法使用'model'? http://jsfiddle.net/lesouthern/8PUtP/5/

.directive('testDirective',function() {
    return {
        restrict : 'A',
        scope : {
            model : '=ngModel'
        },
        link : function($scope,$element,$attrs) {
            $scope.$watch('model',function(x) {
                console.log('this is model: ' + x);
            });
        }
    }
});

1 个答案:

答案 0 :(得分:1)

请参阅this question。这是您更正后的代码:

return {
    restrict : 'A',
    link : function($scope, $element, $attrs) {
        $scope.$watch($attrs.ngModel, function(x) {
            console.log('this is model: ' + x);
        });
    }
}