Skip to content

Instantly share code, notes, and snippets.

@zhouchangxun
Forked from auser/day1.js
Created February 27, 2017 06:05
Show Gist options
  • Save zhouchangxun/3fbc2034f60bea7e31c7dcb6d0011701 to your computer and use it in GitHub Desktop.
Save zhouchangxun/3fbc2034f60bea7e31c7dcb6d0011701 to your computer and use it in GitHub Desktop.
Day1 gist
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