AngularJS:如何运行额外的code AngularJS已呈现模板后? [英] AngularJS: How to run additional code after AngularJS has rendered a template?

查看:194
本文介绍了AngularJS:如何运行额外的code AngularJS已呈现模板后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DOM的角模板。当我的控制器从服务获取新的数据时,它更新在$范围的模型,以及重新呈现的模板。所有好为止。

I have an Angular template in the DOM. When my controller gets new data from a service, it updates the model in the $scope, and re-renders the template. All good so far.

问题是我还需要做一些额外的工作后,模板已经在DOM重新呈现,并(在这种情况下,一个jQuery插件)。

The issue is that I need to also do some extra work after the template has been re-rendered and is in the DOM (in this case a jQuery plugin).

好像应该有一个事件来听,比如一个AfterRender,但我无法找到任何这样的事情。也许一个指令将是很长的路要走,但它似乎火得太早也是如此。

It seems like there should be an event to listen to, such as AfterRender, but I can't find any such thing. Maybe a directive would be a way to go, but it seemed to fire too early as well.

下面是一个概括的jsfiddle我的问题:小提琴,AngularIssue

Here is a jsFiddle outlining my problem: Fiddle-AngularIssue

== ==更新

,我已经相应切换到指令处理DOM操作,并执行指令的内部模型$手表。不过,我还是很具有相同的基本问题;模板之前的$手表事件触发里面的code已被编译并插入到DOM,因此,jQuery插件一直在评估一个空表。

Based on helpful comments, I've accordingly switched to a directive to handle DOM manipulation, and implemented a model $watch inside the directive. However, I still am having the same base issue; the code inside of the $watch event fires before the template has been compiled and inserted into the DOM, therefore, the jquery plugin is always evaluating an empty table.

有趣的是,如果我删除了异步调用整个事情的正常工作,所以这是朝着正确方向迈出的一步。

Interestingly, if I remove the async call the whole thing works fine, so that's a step in the right direction.

下面是我的小提琴更新,以反映这些改变: http://jsfiddle.net/uNREn/12/

Here is my updated Fiddle to reflect these changes: http://jsfiddle.net/uNREn/12/

推荐答案

这帖子是老了,但我改变你的code为:

This post is old, but I change your code to:

scope.$watch("assignments", function (value) {//I change here
  var val = value || null;            
  if (val)
    element.dataTable({"bDestroy": true});
  });
}

请参阅:
http://jsfiddle.net/dnzY9/

我希望它帮助你。

这篇关于AngularJS:如何运行额外的code AngularJS已呈现模板后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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