javascript - How to do base class concept for CRUD in angularjs? -
currently doing student project in angularjs. in need use base class concept crud operation.. need create 1 base class in class contain create, read, update , delete method.. through out application need use common method based on parameter , object save data corresponding location..
create service or factory handle $http
needs. using angular's $http
shortcut methods post
, get
, put
, delete
returns promise
app.factory('dataservice', function($http) { var create = function() { return $http.post("/api/...") // returns promise }; var read = function() { return $http.get("/api/...") }; var update = function() { return $http.put("/api/...") }; var delete = function() { return $http.delete("/api/...") }; return { create: create, read: read, update: update, delete: delete } });
this can re-used throughout application this:
app.controller('mycontroller', function ($scope, dataservice){ dataservice.read().then( function(data){ console.log(data) }, function(error){ console.log(error); }) });
Comments
Post a Comment