I have seen a few exmaples on stack overflow about this ng-init issue, although I cant seem to find one which references it with the use of a controller.
I have called the function in the controller by having the following in the html file
<div class="tab-container" ng-controller = "ExampleController" ng-init = "init()" >
In the controller:
$scope.init = function(){
alert("do something");
};
It does run, but it runs before the components have loaded on the screen.
Am i missing something?
Thanks
ng-init
is supposed to work like this, because it's used to initialize data.A very simple example:
If you are trying to run something while your controller loads, it's actually much simpler than you thought:
Or even simpler, if you don't need the function (no closures or whatever)
Edit - assuming you're using
ngView
:To have the code run on when the page is fully loaded you should set a watcher on the event
$viewContentLoaded
, like this: