Last active
May 2, 2018 10:50
-
-
Save minhphong306/c096537ecb194f6e18590d26e6390bf3 to your computer and use it in GitHub Desktop.
AngularJS basic app + factory + controller (add, update, remove, upload image (angular-file-upload) to do with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// include angular file upload (https://github.com/minhphong306/base_lib/blob/master/angular-file-upload/angular-file-upload.min.js) and ng-thumb | |
var app = angular.module('app', ['angularFileUpload']); | |
// Factory | |
app.factory('mainFactory', function ($http, $rootScope, $q, $log) { | |
var domain = "service/"; | |
var factory = {}; | |
factory.doAction = function (data, uri) { | |
return $http({ | |
url: domain + uri, | |
method: "POST", | |
headers: {'Content-Type': 'application/x-www-form-urlencoded'}, | |
data: jQuery.param(data) | |
}).then(function (response) { | |
return response; | |
}); | |
}; | |
return factory; | |
}); | |
// Controller | |
app.controller('categoryCtrl', function ($scope, mainFactory, $timeout, FileUploader) { | |
var uri = "category.php"; | |
var vm = this; | |
vm.add_model = { | |
"name": "", | |
"description": "" | |
}; | |
vm.edit_model = {}; | |
vm.data = []; | |
vm.decorate = { | |
"is_loading": false | |
}; | |
vm.addData = addData; | |
vm.getData = getData; | |
vm.removeData = removeData; | |
vm.setEditModel = setEditModel; | |
vm.updateData = updateData; | |
getData(); | |
function getData() { | |
var data = { | |
"mode": "get_data" | |
}; | |
vm.decorate.is_loading = true; | |
mainService.doAction(data, uri).then(function (response) { | |
if (!response.data.status) { | |
show_notify('Lỗi', 'Có lỗi xảy ra', 'error'); | |
} else { | |
vm.data = response.data.data; | |
} | |
// For nice display :)) | |
$timeout(function () { | |
vm.decorate.is_loading = false; | |
}, 800); | |
}); | |
} | |
function addData() { | |
var data = { | |
"mode": "add_data", | |
"name": vm.add_model.name, | |
"description": vm.add_model.description | |
}; | |
mainService.doAction(data, uri).then(function (response) { | |
if (!response.data.status) { | |
show_notify('Thông báo', 'Có lỗi xảy ra', 'error'); | |
} else { | |
show_notify('Thông báo', 'Thêm mới thành công', 'success'); | |
getData(); | |
} | |
$("#modalAdd").modal("hide"); | |
}); | |
} | |
function updateData() { | |
var data = { | |
"mode": "update_data", | |
"id": vm.edit_model.id, | |
"name": vm.edit_model.name, | |
"image": vm.edit_model.image, | |
"description": vm.edit_model.description | |
}; | |
mainService.doAction(data, uri).then(function (response) { | |
if (!response.data.status) { | |
show_notify('Thông báo', 'Có lỗi xảy ra', 'error'); | |
} else { | |
show_notify('Thông báo', 'Cập nhật thành công', 'success'); | |
getData(); | |
} | |
$("#modalEdit").modal("hide"); | |
}); | |
} | |
function removeData() { | |
var data = { | |
"mode": "remove_data", | |
"id": vm.edit_model.id | |
}; | |
mainService.doAction(data, uri).then(function (response) { | |
if (!response.data.status) { | |
show_notify('Thông báo', 'Có lỗi xảy ra', 'error'); | |
} else { | |
show_notify('Thông báo', 'Xóa thành công', 'success'); | |
getData(); | |
} | |
$("#modalRemove").modal("hide"); | |
}); | |
} | |
function setEditModel(item) { | |
vm.edit_model = angular.copy(item); | |
} | |
// Upload image with form data | |
vm.data_upload = {}; | |
vm.data_upload['type'] = 'category'; | |
var uploader = $scope.uploader = new FileUploader({ | |
url: 'service/upload.php', | |
formData: [ | |
vm.data_upload | |
], | |
queueLimit: 5 | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment