Requesting JSON content using AngularJS
Angular defines a core object, $http
, which you use to make HTTP requests of remote servers. It's passed to your controller when you initialize it.
How to do it...
Let's extend our controller to add a reference to the $http
object and use it to make a request:
var app = angular.module("aprsapp", []); app.controller("AprsController", ["$scope", "$http", function($scope, $http) { $scope.json = ""; $scope.message = "Loaded..."; $scope.doAjax = function() { $scope.debug = "Fetching..."; $scope.json= ""; $scope.message = ""; var promise = $http({ url: "/", method: "POST", }); }; }]);
Here, we define a function doAjax
in our scope that will perform the asynchronous HTTP request. It updates our models so that the debug
model contains a status message, and the json
and message
models are empty strings. Let's look at the $http
object in more detail.
How it works…
Looking at the controller definition function, you...