How to call function from div on load in angularjs

I am trying to pull value in div element and once I get value I would like to call a function to get data. I am unable to call function inside div element HERe is my Html


<div class="ItemsList" data-ng-init="GetStatsData({{menu.result}})">


function GetStatsData(resultval) {
        .success(function (data, status, headers, config) {
                        $scope.myitems = data

I am getting data when calling function directly but when I try to use div tag and invoke the function I cannot get this working, Its never calling that function. My main goal is to get value in myitems passing menu.result in function.

I appreciate any help to get this resolved


As mentioned in the documentation ngInit
The only appropriate use of ngInit is for aliasing special properties of ngRepeat

You should change the way you you call GetStatsData to be called from the controller as follow

$ = []   // set the array of data here

for(n = 0, n <  $, n++) {
   // wrap you async calls inside function when in loop
   $(function(index) {
       //CALL GetStatsData

The implementation of GetStatsData need little modification

function GetStatsData(menu) {
    .success(function (data, status, headers, config) {
                    menu.myitems = data

Now your view html should be like this

<div class="ItemsList">

Leave a Reply

Your email address will not be published. Required fields are marked *