-
-
Save zhouchangxun/3fbc2034f60bea7e31c7dcb6d0011701 to your computer and use it in GitHub Desktop.
Day1 gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('myApp.services', []) | |
.factory('UserFactory', function($http, $q) { | |
var service = { | |
// our factory definition | |
user: {}, | |
setName: function(newName) { | |
service.user['name'] = newName; | |
}, | |
setEmail: function(newEmail) { | |
service.user['email'] = newEmail; | |
} | |
}; | |
return service; | |
}) | |
.service('UserService', function() { | |
var self = this; // Save reference | |
this.user = {}; | |
this.backendUrl = "http://localhost:3000"; | |
this.setName = function(newName) { | |
self.user['name'] = newName; | |
} | |
this.setEmail = function(newEmail) { | |
self.user['email'] = newEmail; | |
} | |
this.save = function() { | |
return $http.post(self.backendUrl + '/users', { | |
user: self.user | |
}); | |
} | |
}) | |
.provider('User', function() { | |
this.backendUrl = "http://localhost:3000"; | |
this.setBackendUrl = function(newUrl) { | |
if (newUrl) this.backendUrl = newUrl; | |
} | |
// injectables go here | |
this.$get = function($http) { | |
var self = this; | |
var service = { | |
user: {}, | |
setName: function(newName) { | |
service.user['name'] = newName; | |
}, | |
setEmail: function(newEmail) { | |
service.user['email'] = newEmail; | |
}, | |
save: function() { | |
return $http.post(self.backendUrl+'/users', | |
{ | |
user: service.user | |
}); | |
} | |
} | |
return service; | |
} | |
}); | |
angular.module('myApp', ['myApp.services']) | |
.config(function(UserProvider) { | |
UserProvider | |
.setBackendUrl('http://localhost:9000'); | |
}) | |
.controller('PageCtrl', | |
function($scope, User) { | |
$scope.user = "hi"; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment