6
Hello, I would like to know if it is possible with this code to create a service. Because all the videos I see they only create services for things that will be duplicated in code and so on or from localhost links. If you can help me, thank you.
angular.module('TarefApp')
.controller('TarefasController', function($scope) {
$scope.categorias = [
{nome:'Tarefas Primárias'},
{nome:'Tarefas Secundárias'},
];
$scope.tarefas = [];
$scope.categoriaTarefa = {tarefa:{}};
$scope.addTarefa = function(tarefa) {
if(!$scope.categoriaSelecionada){
alert("Selecione uma categoria!")
return;
}
var c = $scope.categoriaSelecionada;
if(!$scope.categoriaTarefa.tarefa[c])
$scope.categoriaTarefa.tarefa[c] = [];
else{
var itemDuplicado = false;
angular.forEach($scope.categoriaTarefa.tarefa[c], function (item, index){
itemDuplicado = (item.nome === tarefa.nome);
if(itemDuplicado){
alert("Tarefa para categoria já existe!");
return false;
}
});
}
if(!itemDuplicado){
$scope.categoriaTarefa.tarefa[c].push(tarefa);
$scope.tarefa = {};
}
};
$scope.delTarefas = function() {
angular.forEach($scope.categorias, function(item) {
var c = item.nome;
var oldTarefas = $scope.categoriaTarefa.tarefa[c];
$scope.categoriaTarefa.tarefa[c] = [];
angular.forEach(oldTarefas, function(tar) {
if (!tar.selecionado) $scope.categoriaTarefa.tarefa[c].push(tar);
});
});
};
$scope.addCategoria = function(categoria) {
for(var i=0; i < $scope.categorias.length; i++){
if($scope.categorias[i].nome === categoria.nome){
alert("A categoria já existe!");
return;
}
}
$scope.categorias.push(angular.copy(categoria));
delete $scope.categoria;
};
});
I believe part of the code is missing at the end that probably makes the code work.
– Larissa Mourullo
@Larissamourullo Err... thank you, you are correct; missed copying the final lines of the service. Post edited and corrected.
– OnoSendai
thank you very much indeed, I found it quite easy as it was done. It helped me a lot.
– Larissa Mourullo
@Larissamourullo Always a pleasure! Having more questions, feel free to ask.
– OnoSendai
I am trying to make it work, but nothing is happening (OBS: no error occurring). I am putting this in the controller to call services: . controller('Tarefascontroller', Function($Scope, Tarefasservice) {
– Larissa Mourullo
@Larissamourullo I changed the post, including an example of control consuming your service.
– OnoSendai