如何角控制器上的文件准备好运行功能? [英] How to run function in angular controller on document ready?
本文介绍了如何角控制器上的文件准备好运行功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的角度控制器内的功能,我想这一功能,必须对文件运行准备好,但我注意到,角运行它作为创建DOM。
I have a function within my angular controller, I'd like this function to be run on document ready but I noticed that angular runs it as the dom is created.
function myController($scope)
{
$scope.init = function()
{
// I'd like to run this on document ready
}
$scope.init(); // doesn't work, loads my init before the page has completely loaded
}
任何人都知道我怎么能去呢?
Anyone know how I can go about this?
推荐答案
我们可以使用 angular.element(文件)。就绪()
方法附加何时回调该文件已准备就绪。我们可以简单地附上回调控制器像这样:
We can use the angular.element(document).ready()
method to attach callbacks for when the document is ready. We can simply attach the callback in the controller like so:
angular.module('MyApp', [])
.controller('MyCtrl', [function() {
angular.element(document).ready(function () {
document.getElementById('msg').innerHTML = 'Hello';
});
}]);
http://jsfiddle.net/jgentes/stwyvq38/1/
这篇关于如何角控制器上的文件准备好运行功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文