A simple Angular demo which uses the http service for retrieving data from github.
Angular and http get service demo
Leave a reply
A simple Angular demo which uses the http service for retrieving data from github.
You can find a simple demo page here for reference.
Your script file should have the following contents:
(function() {
var app = angular.module("SimpleAngularApp", []);
var MainController = function($scope, $http) {
var onUserComplete = function(response) {
$scope.user = response.data;
};
var onError = function(reason) {
$scope.error = "Could not fetch the user";
}
$http.get('https://api.github.com/users/berendjdejong')
.then(onUserComplete, onError);
};
app.controller("MainController", ["$scope", "$http", MainController]);
}());
Then for your html file, which should include the script above and the Angular.js script, the contents should be:
{{user.login}}
{{error}}
Some important things to note:
Below is a code snippet for a Immediately invoked function expression IIFE. An IIFE is used to keep code out of the global namespace.
(function() {
var createWorker = function() {
var workerCount = 0;
var task = function() {
workerCount += 1;
console.log("task " + workerCount);
};
return {
task: task
};
};
var prg = createWorker();
prg.task();
}());