简体   繁体   English

当Angular完成时运行javascript函数

[英]Run a javascript function when Angular has 'finished'

By 'finished', I mean all data binding has completed and there's nothing left for Angular javascript to run (until a user interacts with the DOM). “完成”是指所有数据绑定均已完成,并且Angular javascript无需运行任何操作(直到用户与DOM交互)。

I need to call a javascript function I already have (which, fwiw, simply toggles the display of a few divs). 我需要调用一个已有的javascript函数(fwiw,只需切换几个div的显示)。

What I've Tried 我尝试过的

I want to try and avoid the use of timeouts: 我想尝试避免使用超时:

http://blog.brunoscopelliti.com/run-a-directive-after-the-dom-has-finished-rendering/ http://blog.brunoscopelliti.com/run-a-directive-after-the-dom-has-finished-rendering/

How long angular takes is arbitrary, and depends on too much for me to accurately use time intervals. 角度需要花费多长时间是任意的,并且对我来说准确地使用时间间隔取决于太多。

I've also tried app.run(function().... , but it runs too early!? 我也尝试过app.run(function().... ,但是运行太早了!

EDIT (for clarification) 编辑(为澄清)

Think of this scenario as 2 divs. 将这种情况想象为2格。

<div class="static">SOME CONTENT</div>

and

<div class="angular">{{ someContent }}</div>

On page load (initially), the user will see {{ someContent }} until Angular has 'finished'. 在页面加载(最初)时,用户将看到{{ someContent }}直到Angular完成为止。 I want to hide this angular div, and only show the static div, and then swap them (ie static becomes hidden, and angular becomes visible), once {{ someContent }} has been rendered out. 我想隐藏此角度div,只显示静态div,然后交换它们(即,静态变为隐藏,角度变为可见),一旦{{ someContent }}呈现出来。 It's surely really simple what I'm trying to do, ng-show sounds like it might be a solution - but I'm unsure of how to use it. 我想做的事情确实非常简单, ng-show听起来可能是一个解决方案-但我不确定如何使用它。

If app.run(function() { ... }); 如果app.run(function() { ... }); is too early and you dont want to use $timeout maybe you should refactor the javascript function you want to call and do it the angular way . 为时过早,并且您不想使用$timeout也许您应该重构要调用的javascript函数并以有角度的方式进行操作

If it is just to toggle divs visibility consider using ng-show / ng-hide . 如果仅是切换div可见性,请考虑使用ng-show / ng-hide You can also use ng-if to conditionally remove or recreate a portion of the dom tree. 您还可以使用ng-if有条件地删除或重新创建dom树的一部分。 It depends on the purpose of your function maybe a custom directive is suitable for your case. 这取决于功能的目的,也许自定义指令适合您的情况。

Example in case you want to show some variable data once it gets populated: 如果要在填充变量数据后显示一些变量数据的示例:

<div ng-if='myVar'>
    <span>{{myVar}}</span>
</div>

Updated according to edited question: 根据编辑的问题进行了更新:

<div ng-if='!myVar' class='static'>
    <span>STATIC CONTENT</span>
</div>
<div ng-if='myVar' class='angular'>
    <span>{{myVar}}</span>
</div>

Once again ng-show/ng-hide might work but don't forget that they only toggle the element's visibility. ng-show/ng-hide可能再次起作用,但请不要忘记它们仅切换元素的可见性。 ng-if will remove/recreate the element if the condition is or is not verified. 如果条件已验证或未验证, ng-if将删除/重新创建元素。

//using ng-if
<div ng-if="!someContent" class="static">SOME CONTENT</div>
<div ng-if="someContent" class="angular">{{ someContent }}</div>

//using ng-hide/show
<div ng-hide="someContent" class="static">SOME CONTENT</div>
<div ng-show="someContent" class="angular">{{ someContent }}</div>

This smells like an anti-pattern. 这闻起来像是反模式。 Why exactly do you want toggle those div's? 您为什么要切换这些div? The use of directives might make much more sense in the context. 在上下文中使用指令可能更有意义。

Regardless, if you think you have good reason for doing this, you can schedule a function to run using $applyAsync(yourFunction) . 无论如何,如果您认为这样做有充分的理由,则可以使用$applyAsync(yourFunction)安排要运行的函数。

$applyAsync will queue the function to be executed at a later time. $applyAsync将使该函数排队,以便以后执行。 If you place that during the initialization of your $scope then your function will run after rest of the watchers get executed. 如果在$scope初始化期间放置了该函数,则函数将在其余观察者执行后运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM