HTML加载后调用函数AngularJS [英] Call function after HTML is loaded AngularJS

查看:275
本文介绍了HTML加载后调用函数AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  angular.module('blah')。directive('someDirective',function (){
return {$ b $ scope:{
arrayParam1:'= arrayParam1',
arrayParam2:'= arrayParam2'
},
restrict:' E',
templateUrl:'/my/path/to/someDirective.tpl.html',
link:function(scope,elem,attrs){
//一些逻辑
scope.someEvent = function(){
//一些逻辑
}
作用域。$ on('$ viewContentLoaded',function(){
debugger;
scope.someEvent();
});
}
};
});

现在在HTML中,我对其中一个数组进行了ng重复操作( arrayParam1 例如),我展示了一些东西。现在我们的目标就是在HTML完全加载后(通过ng-repeat的迭代完成)触发 scope.someEvent



当我使用 $ on 时,它甚至不会停止在调试器上。如果我将它更改为 $ watch ,它会在JS中的所有代码完成后停止,但HTML仍未呈现,并且在视图完成之前触发甚至是我想要的。



使用$ timeout。

解决方案

  $ timeout(function(){
scope.someEvent();
});

这将有所帮助。



或if你不想使用 $ timeout 你需要做的是为这个指令创建另一个父指令,并在父母链接阶段执行或触发该函数

Guys I have the following directive :

angular.module('blah').directive('someDirective', function () {
   return {
        scope: {
            arrayParam1: '=arrayParam1',
            arrayParam2: '=arrayParam2'
        },
        restrict: 'E',
        templateUrl: '/my/path/to/someDirective.tpl.html',
        link: function (scope, elem, attrs) { 
            //Some logic
            scope.someEvent = function(){
              //some logic
            }
            scope.$on('$viewContentLoaded', function () {
                debugger;
                scope.someEvent();
            });
        }
   };
});

Now in the HTML I have a ng-repeat over one of the arrays(arrayParam1 for example) and I am display some stuff. Now my goal as you can assume is to trigger the scope.someEvent after the HTML is completely loaded (finished with the iterations of ng-repeat).

Well when I use the $on it does not even stop on the debugger. If I change it to $watch it does stop after all the code is finished in the JS but the HTML is still not rendered and it triggers the even I want before the view is done.

Thanks in advance.

解决方案

use $timeout.

$timeout(function () {
   scope.someEvent();         
});

this will help.

or if you don't want to use $timeout what you have to do is make another directive parent to this directive and execute or trigger the function in parents link phase

这篇关于HTML加载后调用函数AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆