gpt4 book ai didi

angularjs - 在 $scope.$digest 之后立即触发事件

转载 作者:行者123 更新时间:2023-12-03 20:27:55 25 4
gpt4 key购买 nike

在我的 AngularJS 应用程序中,有几个点我想等待 $scope被处理成DOM,然后在上面运行一些代码,就像一个jQuery fadeIn , 例如。
有没有办法收听某种“digestComplete”消息?
我目前的方法是:在设置任何内容后立即$scope我想要呈现的变量,使用 setTimeout延迟 0 毫秒,以便它让范围完成消化,然后运行代码,完美运行。唯一的问题是,我偶尔会在 setTimeout 返回之前看到 DOM 渲染。我想要一种保证在消化之后和渲染之前触发的方法。

最佳答案

在此 jQuery fade-in-and-out fiddle (我在 JSFiddles Examples wiki 页面上找到了它),作者定义了一个“fadey”指令并在指令的链接函数中执行 jQuery 淡入(或淡出)“

<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 :见 this comment米什科 (Miško) 在其中指出:

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 函数。建议/答案再次使用指令。

关于angularjs - 在 $scope.$digest 之后立即触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12102366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com