So, I made a todo list using angularjs
and made an api
using php slim framework
and mysql
for task storage.
I’m trying to use the $http
service to save/delete/update tasks in the DB and so far, I came up with this (code below) but since I’m no expert in this matter, there’s probably a lot of crucial mistakes.
Keep in mind that, the code regarding the todo list works fine and I’d like some guidance and tips for the $http
requests.
JS:
var app = angular.module('todoApp', []);
app.controller('TodoListController', ['$scope', '$http', function ($scope, $http) {
$scope.todos = [];
$scope.newField = [];
$scope.customStyle = {};
$scope.date = new Date();
$http({
method: 'POST',
url: 'http://taskapi.dev/api/tasks/add' }).then(function successCallback (response) {
$scope.addTodo = function () {
$scope.todos.push({text: $scope.todoText, done: false, editing: false, date: new Date()});
$scope.todoText = '';
};
$scope.save = function (index) {
$scope.todos[index].editing = false;
$scope.todos[index].createdOn = new Date().getTime();
};
});
$http({
method: 'PUT',
url: 'http://taskapi.dev/api/tasks/update' }).then(function successCallback (response) {
$scope.editTodo = function (todo) {
todo.editing = true;
};
$scope.change = function (field) {
var todoIndex = $scope.todos.indexOf(field);
$scope.newField[todoIndex] = angular.copy(field);
$scope.todos[todoIndex].editing = true;
$scope.todos[todoIndex].LastModifyOn = new Date().getTime();
};
$scope.turnGreen = function (todo) {
todo.customStyle = {'background': 'green'};
};
$scope.turnYellow = function (todo) {
todo.customStyle = {'background': 'yellow'};
};
$scope.turnRed = function (todo) {
todo.customStyle = {'background': 'red'};
};
$scope.cancel = function (index) {
$scope.todos[index] = $scope.newField[index];
};
});
$http({
method: 'DELETE',
url: 'http://taskapi.dev/api/tasks/delete' }).then(function successCallback (response) {
$scope.delete = function () {
var oldTodos = $scope.todos;
$scope.todos = [];
angular.forEach(oldTodos, function (todo) {
if (!todo.done) $scope.todos.push(todo);
});
};
$scope.remove = function () {
$scope.todos.splice(this.$index, 1);
};
});
}]);
Source: AngularJS
from Angular Questions https://angularquestions.com/2017/09/27/angularjs-http-request-using-rest-api/
via @lzomedia #developer #freelance #web
No comments:
Post a Comment