//An introduction to Single Page Applications
//
//AngularJS - a basic intro
//This is more complex in other environments, but for the purpose of this experiment
//Setup the app
var myApp = angular.module('myApp',[]);
//Create a service - this isn't ultimately necessary but it shows how we can get data in
//and out of a service, like some external datapoint or something similar.
//This one just stores the data to itself, as it is a singleton and maintains state
myApp.service('storageService', ['$rootScope', function($rootScope) {
this.storageObjectArray = [];
this.storeName = function(nameObject) {
this.storageObjectArray.push(nameObject);
return true;
};
this.getStorageArray = function() {
return this.storageObjectArray;
};
}]);
//Start by creating controllers
//The first controller is for collecting user data. Note the variable and dependency injection
myApp.controller('RetrieveAndStore', ['$scope', 'storageService', function RetrieveAndStore($scope, storageService) {
//The scope is a local variable for this contoller. It allows the two-way binding to occur
$scope.logThis = function(name) {
if($scope.name){ //Only run if "name" is populated
//notice we havent actually defined this variable? It was actually defined on the view!
console.log($scope.name);
//ng-bind automatically binds data from the view to the controller (on $scope) and back again
var timeClicked = new Date();
//We are going to call the storageService and put something into the array.
var storeName = storageService.storeName({name: $scope.name, time: timeClicked});
if(storeName){
console.log(storageService.storageObjectArray);
}
//I am doing this to show use of a second function in the service to populate a list of all names stored.
$scope.namesStored = storageService.getStorageArray();
}else{
//This just provides feedback to the user if their name is empty - no point in storing an empty name!
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.