Angular.js – creating a simple controller with a http request and putting the data into local storage

A simple Angular.js example of creating a controller with data form a http request and then put into the local storage.
var app = angular.module('testApp', ['ngStorage']); // you need to add the ngStorage in order to use the internal $localStorage of Angular.js 

app.controller('AppController', function($scope, $http, $localStorage) {

$scope.getData = function() {
		
		
	$http.get('http://yourDomain.com/yourData.json').
	            success(function(response, status, headers, config) {
	    	
	 		$scope.people = response.people;
			$scope.people = $localStorage.peopleData;	
	 		
		    }).
		    error(function(data, status, headers, config) {
		    	
		    	console.log("status fail == " + status);
		    	    	
		    });
		
	}

});

Responses

0 Replies