As someone who is new to angular.js, I am currently exploring the functionalities of $http methods such as $http.get
and $http.post
.
In my simple application, I am retrieving messages from a MySQL database via a Java rest service. The data retrieved displays in the following format:
[
{id:1, content:"Hello World!"},
{id:2, content:"Testing 1, 2, 3..."},
{id:3, content:"I am from the database"}
]
My goal is to list these messages using ng-repeat with the following structure:
<div ng-controller="MessageController">
<ul>
<li ng-repeat="m in messages">{{m.content}}</li>
</ul>
</div>
The corresponding controller code appears as follows:
function MessageController($scope, $http) {
$scope.getAll = function(callback) {
$http.get('/messages').success(function(response) { callback(response); });
}
$scope.messages = $scope.getAll(function(data) { return JSON.stringify(data); });
}
Although I can see that the call successfully returns an array of message objects and I can view them in the console from within the callback function, they are not appearing in the ng-repeat
. Strangely enough, I was able to display them properly when manually generating a list of messages. Is there something I am overlooking?
EDIT: I am utilizing angular.js version 1.2.19