Advertisement
Guest User

Untitled

a guest
Feb 4th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. angular.module("app",[])
  2.  
  3. factory("User",["$http", function( $http ){
  4.  
  5. var f = {};
  6. //read
  7. f.getAll = function(){
  8. return $http.get("users.json", params);
  9. }
  10. //create
  11. f.create = function( params ){
  12. return $http.post("users.json",params);
  13. }
  14. //update
  15. f.update = function(id, params ){
  16. return $http.patch("users/"+id+".json");
  17. }
  18.  
  19. return f;
  20. }])
  21.  
  22. .controller("myController",["$scope","User", function( $scope , User){
  23.  
  24. //reading all users from json api PHP|rails|node etc..
  25. User.getAll().then(
  26. //success callback
  27. function( res ){
  28. $scope.users = res.data; //this will return json data
  29. },
  30. //errror callbacks
  31. function(err){
  32. console.log( err);
  33. }
  34. );
  35. //create
  36. User.create({username:'tearhear18',password:'123123123'}).then(
  37. //success callback
  38. function( res ){
  39. console.log("create success");
  40. },
  41. function(err){
  42. console.log("failed to create");
  43. }
  44. )
  45. //update
  46. User.update(1,{password:abcdef}).then(){
  47. function( res ){
  48. console.log("create success");
  49. },
  50. function(err){
  51. console.log("failed to create");
  52. }
  53. }
  54.  
  55. }])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement