Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////
- // app.js
- /////////////////////////////////////////////
- var commentApp = angular.module('commentApp',['mainCtrl','AddWorkServices'],function($interpolateProvider){
- $interpolateProvider.startSymbol('<%');
- $interpolateProvider.endSymbol('%>');
- });
- /////////////////////////////////////////////
- // mainCtrl.js
- /////////////////////////////////////////////
- angular.module('mainCtrl',[])
- .controller('mainController',function($scope,$http,Comments){
- $scope.commentData = {};
- $scope.loading = true;
- // ดึง service ทั้งหมด factory เข้าไปใน scope comments
- Comments.get()
- .success(function(data){
- $scope.comments = data;
- $scope.loading = false;
- });
- // รองรับการ submit forms
- $scope.submitComment = function(){
- $scope.loading = true;
- Comments.save($scope.commentData)
- .success(function(data){
- Comments.get()
- .success(function(getData){
- $scope.comments = getData;
- $scope.loading = false;
- });
- })
- .error(function(data){
- console.log(data);
- });
- };
- // ลบ comment
- $scope.deleteComment = function(id){
- $scope.loading = true;
- // เรียก function ชื่อว่า destroy จาก services
- Comments.destroy(id)
- .success(function(data){
- Comments.get()
- .success(function(getData){
- $scope.comments = getData;
- $scope.loading = false;
- });
- });
- };
- });
- /////////////////////////////////////////////
- // commentServices
- /////////////////////////////////////////////
- angular.module('commentServices',[])
- .factory('Comments',function($http){
- return{
- get : function(){
- return $http.get('/api/comments');
- },
- show : function(id){
- return $http.get('api/comments/'+id);
- },
- save : function(commentData){
- return $http({
- method : 'POST',
- url: '/api/comments',
- headers: {'Content-Type' : 'application/x-www-form-urlencoded' } ,
- data: $.param(commentData)
- });
- },
- destroy : function(id){
- return $http.delete('/api/comments/'+id);
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement