呼叫功能的角度毕竟指令是加载 [英] Call function after angular all directives are loaded

查看:147
本文介绍了呼叫功能的角度毕竟指令是加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的ngApp多个自定义指令。该演示code是:

I have multiple custom directives in my ngApp. The demo code is:

<top-nav></top-nav>
<left-sidebar></left-sidebar>
<div class="content">
    ....
</div>

需要'左边栏'的高度,以根据顶导览'的高度进行调节。
同样还有其他的相互依存UI任务。我想运行负载后code(说app.initializeUI();)只有在所有的指令已经被加载和渲染。

The height of the 'left-sidebar' needs to be adjusted according to the height of the 'top-nav'. Similarly there are other interdependent UI tasks. I want to run the post-load code (say app.initializeUI();) only after ALL the directives have been loaded and rendered.

现在,我如何做到这一点?

Now, How do I achieve that?

编辑:

目前我使用下面的code:

Currently I am using the following code :

App.run(function($timeout){
    $timeout(function(){ app.init() },0);
    });

这运行正常,但是,我不知道这是这样做的最佳方式。

This runs fine, however, I am not sure this is the perfect way of doing this.

推荐答案

我想创建一个孤立的范围,设置端子= TRUE 属性指令,并把它添加到你的身体标签。然后在你的链接功能,设置一个$表 - 将IsInitialized 这最初是假的。在你的$表处理,请与 app.init(),然后去注册$手表,因此,它总是初始化一次。

I would create an attribute directive with isolated scope, set terminal=true and add it to your body tag. Then in your link function, setup a $watch - isInitialized which is initially false. In your $watch handler, call your app.init(), and then de-register the $watch, so that it is always initialized once.

设置一个终端指令是有后果的 - 没有其他的指令可以在相同的元素终端指令后运行。确保这是你想要的。另一种方法是给你的指令可能的最低值,以便它首先编译,和最后的已链接。

Setting up a terminal directive has consequences - no other directive can run after the terminal directive on the same element. Make sure this is what you want. An alternative is to give your directive the lowest possible value so that it is compiled first, and linked last.

的重要的部分在该溶液是:

The important pieces to this solution are:


  1. 将增加你的指令body标签,可以确保它是最后的联系。

  2. 将增加一个$手表,你必须确保所有其他指令都通过你的$表处理程序被调用,其他所有指令应该已经呈现时间通过消化周期走了,就这样。

谨慎注:摘要周期可能会运行几次变化范围stabalise之前。上述解决方案仅在第一个周期,你想要什么,如果你真的想最终渲染这可能不是后运行。

Note of caution: The digest cycle may run several times before scope changes stabalise. The above solution only runs after the first cycle, which may not be what you want if you really want the final rendering.

这篇关于呼叫功能的角度毕竟指令是加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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