为什么ng-style调用2次函数绑定它?

Jim*_*ane 1 angularjs angularjs-directive

我正在使用ng-style在我的一个指令中传递样式.

像这样:

<my-component ng-style="test()" ng-model="message"></my-component>
Run Code Online (Sandbox Code Playgroud)

指令:

myModule.directive('myComponent', function(mySharedService) {
    return {
        restrict: 'E',
        controller: function($scope, $attrs, mySharedService) {
            $scope.test = function(){
                console.log(1)
            };
            $scope.$on('handleBroadcast', function() {
                $scope.message = 'Directive: ' + mySharedService.message;
            });
        },
        replace: true,
        template: '<input>'
    };
});
Run Code Online (Sandbox Code Playgroud)

JSFIDDLE记录2次'1'

为什么test函数调用了2次?

Ste*_*ers 6

一旦获得该值,再次查看它是否已更改.这就是手表的工作方式,每个应用消化周期都会处理手表!这意味着每个apply-digest循环至少调用一次watch表达式,最多10次,这是Angular团队强制执行的硬限制.也许这张照片清除了一点!从监视列表返回到eval队列的箭头是相关位.

HTH!

  • 每次运行$ digest循环时,都会调用该函数. (2认同)