加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 为什么我必须调用$scope?$digest()在这里?

发布时间:2020-12-17 07:48:44 所属栏目:安全 来源:网络整理
导读:我创建了一个显示工具提示的指令: app.directive('tooltip',function(){ return{ restrict: 'A',link: function(scope,element,attr){ element.bind('mouseenter',function(e){ scope.setStyle(e); }); } }}); 相应的setStyle()函数: $scope.setStyle = fu
我创建了一个显示工具提示的指令:
app.directive('tooltip',function(){
    return{
        restrict: 'A',link: function(scope,element,attr){
            element.bind('mouseenter',function(e){

                scope.setStyle(e);

            });
        }
    }
});

相应的setStyle()函数:

$scope.setStyle = function(e){
    $scope.style = {
        position: 'absolute',// some other styles
    };

    $scope.$digest();
};

$scope.style应用于此:

<span ng-style="style">I am a tooltip</span>

这是我看法的一部分,由拥有$scope.style的控制器处理

为什么我必须调用$digest()才能将更改应用到$scope.style,这是先前声明和初始化的?

因为附加到mouseenter事件的回调超出了角度的范围;角度不知道该函数何时运行/结束,所以摘要循环永远不会运行.

调用$digest或$apply可以更改绑定并触发任何手表.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读