Running a factory in AngularJS
angular.module('app.services',[])
.factory('myFactory', function($http){
return {
getData: function(){
return {animal: 'dog'}
},
isUser: function() {
var url='http://parleyvale.com/isUser/';
var promise=$http.get(url).
success(function(data, status) {
console.log(data);
console.log(status);
return data;
}).
error(function(data, status){
console.log(data || "Request failed");
console.log(status);
var data = {message: "Server is down"}
return data;
});
return promise;
}, ...
Utilizing data from the factory in a controller
angular.module('app.controllers',[])
.controller('MainCtrl', ['$scope', 'myFactory', function($scope, myFactory){
$scope.factoryOutput=myFactory.getData();
$scope.isUser=myFactory.isUser().then(function(data){
console.log(data.data.items);
$scope.factoryOutput2=data.data.items
}); ...
Challenges when server is down
x GET http://parleyvale.com/isUser/ net::ERR_CONNECTION_REFUSED
Seeking solution for handling errors
Explore http://jsbin.com/nebed/2/edit