Melhor forma de organizar o código angularjs
Criei um api dentro de um arquivo de controle de uma aplicação em agularjs. Queria saber como colocar o conteúdo dessa api (variável $scope.apiClient) em um outro arquivo? Um service por exemplo e deixar somente as funções que pertinentes ao controller.
module.exports = function($scope, $http, $filter, apiClientService) {
$scope.name = $filter("uppercase")("My Pizza");
$scope.day = new Date();
$scope.total = 27.35;
$scope.editing = false;
$scope.msg = "";
$scope.clients = [];
$scope.apiClient = {
url: "[url=[http://localhost/cursos/SON/angulajs/angulajs-avancado/curso/server/apiCliente.php](http://localhost/cursos/SON/angulajs/angulajs-avancado/curso/server/apiCliente.php)][http://localhost/cursos/SON/angulajs/angulajs-avancado/curso/server/apiCliente.php](http://localhost/cursos/SON/angulajs/angulajs-avancado/curso/server/apiCliente.php)",[/url]
// Lista os clientes
all: function () {
$http.get(this.url).success(function (response) {
$scope.clients = response;
});
},
// Adiciona um novo cliente
add: function (client) {
$http.post(this.url, client).success(function () {
$scope.apiClient.all();
});
},
// Deleta um cliente
delete: function (client) {
$http.delete(this.url, {params: {
clientId: client.id
}}).success(function () {
$scope.apiClient.all();
});
},
// Edita um cliente
edit: function (client) {
$http.put(this.url, client).success(function () {
$scope.apiClient.all();
});
}
};
// Init
apiClientService.api.all();
// Add new client
$scope.addClient = function (client) {
apiClientService.api.add(angular.copy(client));
$scope.formClient.$setPristine();
delete $scope.client;
};
// Edit client
$scope.editClient = function (client) {
$scope.client = client;
$scope.editing = true;
};
// Save client
$scope.saveClient = function () {
console.log($scope.client);
apiClientService.api.edit(angular.copy($scope.client));
$scope.formClient.$setPristine();
delete $scope.client;
$scope.editing = false;
};
// Delete client
$scope.destroyClient = function (client) {
apiClientService.api.delete(client);
};
// Order table
$scope.orderTable = function (column) {
$scope.column = column;
$scope.order = !$scope.order;
};
};Discussão (1)
Carregando comentários...