Sarang Khajuria Sarang Khajuria - 29 days ago 8
AngularJS Question

Loading data in ng-repeat after $http

I am trying to load a number of elements in a div from json via $http call. But whatever I try the list remains empty. Here is the code.

<div class="submenu" >
<div class="menuitem" ng-repeat="item in navigator" >{{item}}
<a href="#/main/welcome/{{item.messageId}}">{{item.label}}</a>
</div>


It is an abstract state and I have assigned controller greetingctrl to it in routes. The JS code is

app.controller('greetingCtrl',function($scope,$cookieStore,$timeout,$state,msg,SERVICE,RestService,$stateParams){

$scope.list = [];
$scope.navigator;

$scope.options.loading = false;
var currentmsg;
$scope.user = $cookieStore.get('userinfo');


$scope.init = function(){
var url = SERVICE.greetingMsg;
var data = {
providerID : $cookieStore.get('userinfo').providerId,
loginID : $cookieStore.get('userinfo').loginId,
action : "GET_MESSAGE"
}
RestService.post(url,data).then(function(res){
$scope.navigator = res.messages;
},
function(err){
console.log(err);

});
}


Here I can successfully receive the data but the data in the list never comes. RestService.post is $http method I have defined in a service.I am using angular 1.5.8 and I am new to angular.

EDIT: Removed ng-show and ng-if. Still the data is nnot showing

Answer

You need to check two variables:
- showmenu I don't see its value changing at all so it's always false.
- loadingisDone same as the previous.

And remember to call $scope.init somewhere.