I am new to angularjs and I have a very small app that loads contacts from my server. The code snippet here is the complete app.js. The problem is that I can't figure out how to do the server call with sync. In the code snippet, when I refresh the page, alert 3 is displayed, then alert 2, and then finally alert 4. The function is immediately returning since the server http call take some time to do. So what I get in the browser is a display of the 2 test item in the array "contacts". Alert 4 finally comes along, but it is too late. Any help would be appreciated.
var module = angular.module('app', []);
module.service('ContactService', function ($http) {
//contacts array to hold list of all contacts - 2 entries for test
//var $contacts = [];
var contacts = [
{
id: 0,
'First_Name': 'Harmon',
'Last_Name': 'Adams',
'Home_Phone': '123-2343-44'
},
{
id: 1,
'First_Name': 'Sam',
'Last_Name': 'Spade',
'Home_Phone': '123-2343-44'
}
];
// returns the contacts list
this.list = function () {
// var contacts = [];
$http.post('http://localhost/Contacts7/GetData.php', {'cat' : 'Friends'}).
success(function(data) {
contacts = data.datarecords;
alert('4 - within $post - '+contacts);
}).
error(function(data, status){
alert('error!');
});
alert('3 - before return - '+contacts);
return contacts;
}
});
module.controller('ContactController', function ($scope, ContactService ) {
$scope.contacts = ContactService.list();
alert('2 - after list - '+ $scope.contacts);
});
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Contact Dialer </title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div ng-app="app" ng-controller="ContactController">
<div class="container" >
<div class="row row-centered">
<div class="col-md-2 button-row col-centered">
<button type="button" class="btn btn-Primary btn-l btn-block" ng-click="GetByCat('Favorites')">Favorites</button>
</div>
<div class="col-md-2 button-row col-centered">
<button type="button" class="btn btn-Primary btn-l btn-block" ng-click="GetByCat('Friends')">Friends</button>
</div>
<div class="col-md-2 button-row col-centered">
<button type="button" class="btn btn-Primary btn-l btn-block" ng-click="GetByCat('Loose Friends')">Loose Friends</button>
</div>
<div class="col-md-2 button-row col-centered">
<button type="button" class="btn btn-Primary btn-l btn-block" ng-click="GetByCat('Loose_Loose Friends')">Loose-Loose Friends</button>
</div>
<div class="col-md-2 button-row col-centered">
<button type="button" class="btn btn-Primary btn-l btn-block" ng-click="GetByCat('Business')">Business</button>
</div>
<div class="col-md-2 button-row">
</div>
</div>
{{xxx}}
<table class='table table-striped table-bordered'>
<th class = 'text-center'>Name</th>
<th class = 'text-center'>Home Phone</th>
<th class = 'text-center'>Mobile Phone</th>
<th class = 'text-center'>Bus. Phone</th>
</tr>
<tr ng-repeat='contact in contacts'>
<th class = 'text-center' >{{contact.Last_Name}}, {{contact.First_Name}}</th>
<td class = 'text-center'>{{contact.Home_Phone}}</td>
<td class = 'text-center'>{{contact.Mobile_Phone}}</td>
<td class = 'text-center'>{{contact.Business_Phone}}</td>
</tr>
</table></div>
</div>
<script src="js/jquery-1.11.0.min.js"></script>
<script src="js/underscore-min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/angular.min.js"></script>
<script src="js/angular-route.min.js"></script>
<script src="js/app.js"></script>
</body>
</h