Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- angular.module("app",[])
- factory("User",["$http", function( $http ){
- var f = {};
- //read
- f.getAll = function(){
- return $http.get("users.json", params);
- }
- //create
- f.create = function( params ){
- return $http.post("users.json",params);
- }
- //update
- f.update = function(id, params ){
- return $http.patch("users/"+id+".json");
- }
- return f;
- }])
- .controller("myController",["$scope","User", function( $scope , User){
- //reading all users from json api PHP|rails|node etc..
- User.getAll().then(
- //success callback
- function( res ){
- $scope.users = res.data; //this will return json data
- },
- //errror callbacks
- function(err){
- console.log( err);
- }
- );
- //create
- User.create({username:'tearhear18',password:'123123123'}).then(
- //success callback
- function( res ){
- console.log("create success");
- },
- function(err){
- console.log("failed to create");
- }
- )
- //update
- User.update(1,{password:abcdef}).then(){
- function( res ){
- console.log("create success");
- },
- function(err){
- console.log("failed to create");
- }
- }
- }])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement