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

事件 – AngularJS:在$scope之后立即触发事件$digest

发布时间:2020-12-17 07:47:15 所属栏目:安全 来源:网络整理
导读:在我的AngularJS应用程序中,我想要等待一个$scope被处理到DOM中,然后运行一些代码,如jquery fadeIn,几个点. 有没有办法听取一些digestComplete消息呢? 我目前的方法是,在设置了我想要渲染的任何$scope变量之后,使用setTimeout延迟0 ms,这样它就可以让范围完
在我的AngularJS应用程序中,我想要等待一个$scope被处理到DOM中,然后运行一些代码,如jquery fadeIn,几个点.

有没有办法听取一些digestComplete消息呢?

我目前的方法是,在设置了我想要渲染的任何$scope变量之后,使用setTimeout延迟0 ms,这样它就可以让范围完成消化,然后运行代码,这样就能很好地运行.唯一的问题是,我非常偶尔看到DOM渲染之前setTimeout返回.我想要一种在消化之后和渲染之前保证触发的方法.有任何想法吗?

在这个 jQuery fade-in-and-out fiddle(我在 JSFiddles Examples维基页面上找到它),作者定义了一个“fadey”指令,并在指令的链接函数中执行jQuery fadeIn(或fadeOut)
<li ng-repeat="item in items" fadey="500">
...
myApp.directive('fadey',function() {
return {
    restrict: 'A',link: function(scope,elm,attrs) {
        var duration = parseInt(attrs.fadey);
        if (isNaN(duration)) {
            duration = 500;
        }
        elm = jQuery(elm); // this line is not needed if jQuery is loaded before Angular
        elm.hide();
        elm.fadeIn(duration)

另一个可能的解决方案是使用$evalAsync:见Mi?ko的this comment,他在其中指出:

The asyncEval is after the DOM construction but before the browser renders.
I believe that is the time you want to attach the jquery plugins. otherwise
you will have flicker. if you really want to do after the browser render
you can do $defer(fn,0);

($defer被重命名为$timeout).

但是,我认为使用一个指令(因为你操纵DOM)是更好的方法.

这是一个SO post,OP尝试在范围上监听$viewContentLoaded事件(这是另一个选择),以便应用一些jQuery函数.建议/答复再次是使用指令.

(编辑:李大同)

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

    推荐文章
      热点阅读