I'm trying to remove an item from a table, however it returns this error:
TypeError: Can not read property 'endGoal' of undefined at $ childScopeClass. $ scope.removeGoal
follows the part of the angle where the error occurs:
$scope.removeGoal = function (goal) {
var index = $scope.indicator.indicatorGoals.indexOf(goal);
if ($scope.indicator.indicatorGoals.length-1 > 0) {
if ($scope.indicator.indicatorGoals[index].endGoal == null) {
$scope.indicator.indicatorGoals[index - 1].endGoal = null;
}
else
{
$scope.indicator.indicatorGoals[index - 1].endGoal =
$scope.indicator.indicatorGoals[index].endGoal;
}
}
$scope.indicator.indicatorGoals.splice(index, 1);
};
Full Code
/// <reference path="../../_references.js" />
/// <reference path="../registrationModel.js" />
(function () {
angular.module("registrationModule").controller("IndicatorFormController",
['$scope', '$filter', 'dateService', 'indicatorRepository', 'selectionContentService', '$compile', 'indicatorGoalRepository',
function ($scope, $filter, dateService, indicatorRepository, selectionContentService, $compile, indicatorGoalRepository) {
$scope.frequencies = [
{ id: 1, name: 'Diário' },
{ id: 2, name: 'Semanal' },
{ id: 3, name: 'Mensal' },
{ id: 4, name: 'Anual' },
];
//Process
$scope.selectedProcesses = [];
$scope.processesToAdd = [];
$scope.processesToRemove = [];
//Branches
$scope.branchesToAdd = [];
$scope.branchesToRemove = [];
$scope.selectedBranches = [];
//Users
$scope.usersToAdd = [];
$scope.usersToRemove = [];
$scope.selectedUsers = [];
$scope.indicator = {
active: true,
iso: true,
indicatorGoals: []
};
//Inicializa o indicar com o mes e ano iniciais
if (typeof (Indicator) != 'undefined' && Indicator != null) {
$scope.indicator = Indicator;
$scope.indicator.indicatorGoals.sort(function (a, b) {
var keyA = new Date(a.startGoal),
keyB = new Date(b.startGoal);
// Compare the 2 dates
if (keyA < keyB) return -1;
if (keyA > keyB) return 1;
return 0;
});
$scope.branchesToAdd = $scope.indicator.branches;
$scope.indicator.branches = [];
$scope.processesToAdd = $scope.indicator.processes;
$scope.indicator.processes = [];
$scope.usersToAdd = $scope.indicator.responsibleUsers;
$scope.indicator.responsibleUsers = [];
console.log($scope.usersToAdd)
console.log($scope.indicator.indicatorGoals)
if ($scope.indicator.endCollect != null) {
var old_value = angular.copy($scope.indicator.endCollect.replace("-", "").replace("-", "").substring(0, 8));
var day = old_value.substring(6, 8) + "/";
var mes = old_value.substring(4, 6) + "/";
var year = old_value.substring(0, 4);
old_value = day + mes + year;
}
}
//Pega os itenvalos [trimestral, semestral] ...
$scope.intervals = dateService.intervals;
$scope.intervals.push({ id: 5, name: "Meses Consecutivos" });
if ($scope.indicator.name != null) {
if ($scope.indicator.endCollect == null)
$("#div-endless").hide();
}
$("input#active").live("click", function () {
if ($(this).is(":checked")) {
$("#endless").prop("disabled", true);
$("input#inp_endless").val("");
$("#div-endless").hide();
} else {
$("#endless").prop("disabled", false);
$("input#inp_endless").val(old_value);
$("#div-endless").show();
}
});
$scope.addProcesses = function () {
var modal = $(this).getModalWindow();
//Get Process Selection
selectionContentService.getProcessSelectionContent().then(function (content) {
$.modal({
content: content,
title: 'Preencha o formulário',
maxHeight: 480,
width: 660,
buttons: {
"Selecionar": function (wind) {
$("#selectableProcessList input:checked").each(function () {
//var partes = $(this).val().split(";");
//var id = parseInt(partes[0], 10);
//var name = partes[1];
var id = parseInt($(this).val());
var name = $(this).data('text');
$scope.processesToAdd.push({ processID: id, name: name });
$scope.$apply();
});
modal.show();
wind.closeModal();
},
"Cancelar_red": function (wind) { modal.show(); wind.closeModal(); }
},
onClose: function () {
modal.show();
}
});
});
};
$scope.removeProcesses = function () {
angular.forEach($scope.selectedProcesses, function (v) {
var index = $scope.processesToAdd.indexOf(v);
$scope.processesToRemove.push(v);
$scope.processesToAdd.splice(index, 1);
});
};
$scope.addBranches = function () {
var modal = $(this).getModalWindow();
//Get Branch Selection
//true para pegar com multipleselects [checkboxes] false para [radio]
selectionContentService.getBranchSelectionContent(true, false).then(function (content) {
$.modal({
content: content,
title: 'Selecione as unidades',
maxHeight: 480,
width: 660,
buttons: {
"Selecionar": function (wind) {
$("#selectableBranchList input:checked").each(function () {
var partes = $(this).val().split(";");
var id = parseInt(partes[0], 10);
var name = partes[1];
$scope.branchesToAdd.push({ branchID: id, name: name });
$scope.$apply()
});
modal.show();
wind.closeModal();
},
"Cancelar_red": function (wind) { modal.show(); wind.closeModal(); }
},
onClose: function () {
modal.show();
}
});
});
};
$scope.removeBranches = function () {
angular.forEach($scope.selectedBranches, function (v) {
var index = $scope.branchesToAdd.indexOf(v);
$scope.branchesToRemove.push(v);
$scope.branchesToAdd.splice(index, 1);
});
};
$scope.addUsers = function () {
var modal = $(this).getModalWindow();
//Get User Selection
//true para pegar com multipleselects [checkboxes] false para [radio]
selectionContentService.getUserSelectionContent(true).then(function (content) {
$.modal({
content: content,
title: 'Selecio os usuários',
maxHeight: 480,
width: 660,
buttons: {
"Selecionar": function (wind) {
$("#selectableUserList input:checked").each(function () {
var id = parseInt($(this).val());
var name = $(this).data('text');
$scope.usersToAdd.push({ personID: id, name: name });
$scope.$apply();
});
modal.show();
wind.closeModal();
},
"Cancelar_red": function (wind) { modal.show(); wind.closeModal(); }
},
onClose: function () {
modal.show();
}
});
});
};
$scope.removeUsers = function () {
angular.forEach($scope.selectedUsers, function (v) {
var index = $scope.usersToAdd.indexOf(v);
$scope.usersToRemove.push(v);
$scope.usersToAdd.splice(index, 1);
});
};
$scope.addGoal = function () {
selectionContentService.getIndicatorGoalForm().then(function (content) {
var modal = $(this).getModalWindow();
$.modal({
title: 'Cadastro de Metas',
content: content,
width: 600,
maxHeight: 480,
onOpen: function () {
$compile($(this))($scope);
},
buttons: {
'Salvar': function (wind) {
clearErrorsMessages();
var scopeChild = $scope.$$childTail;
var indicatorGoal = scopeChild.indicatorGoal;
var oldGoal = scopeChild.indicatorGoal;
scopeChild.indicatorGoalErrors = [];
indicatorGoalRepository.save(oldGoal, indicatorGoal).then(
function (res) {
oldGoal = angular.copy($scope.indicator.indicatorGoals[$scope.indicator.indicatorGoals.length-1]);
//if ($scope.indicator.indicatorGoals.length > 1) {
// //$scope.indicator.indicatorGoals[$scope.indicator.indicatorGoals.length - 2].endGoal =
// // $scope.indicator.indicatorGoals[$scope.indicator.indicatorGoals.length - 1].startGoal;
//}
if ($scope.indicator.indicatorGoals.length >= 1) {
//oldGoal.endGoal = indicatorGoal.startGoal;
//Arrumo o formato da data
var correctDate = indicatorGoal.startGoal.replace("-", " ").replace("-", " ").substring(0, 10);
var newData = new Date(correctDate);
//Tira um dia da data
newData = newData.setDate(newData.getDate() - 1);
//recebe a data
oldGoal.endGoal = newData;
//limpa o scope anterior
$scope.indicator.indicatorGoals.splice(($scope.indicator.indicatorGoals.length - 1), 1);
//Adicionar o scope anterior editado um o novo
$scope.indicator.indicatorGoals.push(oldGoal, indicatorGoal);
}
else {
$scope.indicator.indicatorGoals.push(indicatorGoal);
}
//$scope.save(false, $scope.indicator);
modal.show();
wind.closeModal();
},
function (errors) {
scopeChild.indicatorGoalErrors = errors;
scopeChild.validationFields();
}
);
//var indicatorGoalForm = $scope.$$childTail.indicatorGoalForm;
//$scope.$$childTail.saveGoal();
//if (indicatorGoalForm.$valid) {
// var lastGoal = $scope.indicator.indicatorGoals.length > 0 ? $scope.indicator.indicatorGoals[$scope.indicator.indicatorGoals.length - 1] : null;
// if (lastGoal != null) {
// var aux = angular.copy(lastGoal);
// var startDate = angular.copy($scope.$$childTail.indicatorGoal.startGoal);
// startDate = new Date(startDate.setDate(startDate.getDate() - 1));
// aux.endGoal = angular.copy(startDate);
// $scope.removeAndAdd(lastGoal, aux);
// }
// var newIndicatorGoal = angular.copy($scope.$$childTail.indicatorGoal);
// $scope.indicator.indicatorGoals.push(newIndicatorGoal);
// $scope.$digest();
//var newIndicatorGoal = $scope.$$childTail.indicatorGoal;
//$scope.$apply(function () {
// $scope.indicator.indicatorGoals.push(newIndicatorGoal);
//});
//modal.show();
//wind.closeModal();
},
'Cancelar_red': function (wind) {
$scope.$$childHead = null;
modal.show();
wind.closeModal();
}
},
onClose: function () {
$scope.$$childHead = null;
}
});
});
};
$scope.isLastGoal = true;
$scope.editGoal = function (goal) {
var modal = $(this).getModalWindow();
scopeChild = $scope.$$childTail;
indicatorGoal = scopeChild.indicatorGoal;
lastGoal = scopeChild.indicatorGoal;
scopeChild.indicatorGoalErrors = [];
//Pega o index do scope
var index = $scope.indicator.indicatorGoals.indexOf(goal);
$scope.indicatorGoal = angular.copy($scope.indicator.indicatorGoals[index]);
//$scope.indicatorGoal = $scope.indicator.indicatorGoals[index];
var scopeChild = $scope.$$childTail;
$scope.indicatorGoal = scopeChild.indicatorGoal;
var lastGoal = angular.copy($scope.indicator.indicatorGoals[index-1]);
//var lastGoal = $scope.indicator.indicatorGoals[index-1];
//$scope.isLastGoal = $scope.indicator.indicatorGoals.indexOf(goal) == ($scope.indicator.indicatorGoals.length - 1);
selectionContentService.getIndicatorGoalForm().then(function (content) {
$.modal({
title: 'Cadastro de Metas',
content: content,
width: 600,
maxHeight: 480,
onOpen: function () {
$compile($(this))($scope);
},
buttons: {
'Salvar': function (wind) {
var correctDate = $scope.indicatorGoal.startGoal.replace("-", " ").replace("-", " ").substring(0, 10);
var newData = new Date(correctDate);
if (index > 0) {
//Tira um dia da data
newData = newData.setDate(newData.getDate() - 1);
//recebe a data
lastGoal.endGoal = newData;
$scope.indicator.indicatorGoals.splice((index - 1), 1, lastGoal);
}
$scope.indicator.indicatorGoals.splice((index),1,$scope.indicatorGoal);
$scope.$apply();
modal.show();
wind.closeModal();
},
'Cancelar_red': function (wind) {
$scope.$$childHead = null;
$scope.indicatorGoal = null;
wind.closeModal();
}
},
onClose: function () {
$scope.indicatorGoal = null;
$scope.$$childHead = null;
}
});
});
};
$scope.removeGoal = function (goal) {
var index = $scope.indicator.indicatorGoals.indexOf(goal);
if ($scope.indicator.indicatorGoals.length-1 > 0) {
if ($scope.indicator.indicatorGoals[index].endGoal == null) {
$scope.indicator.indicatorGoals[index - 1].endGoal = null;
}
else
{
$scope.indicator.indicatorGoals[index - 1].endGoal = $scope.indicator.indicatorGoals[index].endGoal;
}
}
$scope.indicator.indicatorGoals.splice(index, 1);
};
$scope.removeAndAdd = function (goal, adds) {
var index = $scope.indicator.indicatorGoals.indexOf(goal);
$scope.indicator.indicatorGoals.splice(index, 1, adds);
};
$scope.cancel = function () {
};
var validationFields = function () {
var scopeForm = $scope.indicatorForm;
var form = indicatorForm;
if (scopeForm.name.$invalid) {
angular.element(form.name).addClass(validationClassErrosType.input);
}
validationCheckImage(angular.element(form.name), angular.element(form.name).parent());
if ($scope.indicator.measureUnitID == 0) {
angular.element(form.measureUnit).addClass(validationClassErrosType.input);
}
validationCheckImage(angular.element(form.measureUnit), angular.element(form.measureUnit).parent());
if ($scope.processesToAdd.length == 0) {
angular.element(form.processes).addClass(validationClassErrosType.input);
}
validationCheckImage(angular.element(form.processes), angular.element(form.processes).parent());
if ($scope.branchesToAdd.length == 0) {
angular.element(form.branches).addClass(validationClassErrosType.input);
}
validationCheckImage(angular.element(form.branches), angular.element(form.branches).parent());
if ($scope.usersToAdd.length == 0) {
angular.element(form.users).addClass(validationClassErrosType.input);
}
validationCheckImage(angular.element(form.users), angular.element(form.users).parent());
if (scopeForm.intervalAnalysis.$invalid) {
angular.element(form.intervalAnalysis).addClass(validationClassErrosType.input);
}
validationCheckImage(angular.element(form.intervalAnalysis), angular.element(form.intervalAnalysis).parent());
if (scopeForm.consecutiveMonths.$invalid) {
angular.element(form.consecutiveMonths).addClass(validationClassErrosType.input);
}
validationCheckImage(angular.element(form.consecutiveMonths), angular.element(form.consecutiveMonths).parent());
if (scopeForm.frequency.$invalid) {
angular.element(form.frequency).addClass(validationClassErrosType.input);
}
validationCheckImage(angular.element(form.frequency), angular.element(form.frequency).parent());
if (scopeForm.startCollect.$invalid) {
angular.element(form.startCollect).parent().addClass(validationClassErrosType.input);
}
if (scopeForm.endCollect.$invalid) {
angular.element(form.endCollect).parent().addClass(validationClassErrosType.input);
}
if ($scope.indicator.indicatorGoals.length == 0) {
angular.element($("#goals")).addClass(validationClassErrosType.genericTable);
}
for (var i = 0; i < $scope.indicatorErros.length; i++) {
if ($scope.indicatorErros[i] == "A data inicial do indicador é maior que as datas das metas.") {
angular.element(form.startCollect).parent().addClass(validationClassErrosType.input);
angular.element($("#goals")).addClass(validationClassErrosType.genericTable);
}
if ($scope.indicatorErros[i] =="A data de início de uma nova meta é depois da data final do indicador.") {
angular.element(form.endCollect).parent().addClass(validationClassErrosType.input);
}
}
validationCheckImage(angular.element(form.endCollect).parent(), angular.element(form.endCollect).parent());
validationCheckImage(angular.element(form.startCollect).parent(), angular.element(form.startCollect).parent());
validationCheckImage($("#goals"), $("#goals"), validationClassErrosType.genericTable);
};
$scope.indicatorErros = [];
//Save
$scope.save = function (indicator) {
clearErrorsMessages();
$scope.indicatorErros = [];
var objectToSave = angular.extend({},
indicator,
{ usersToAdd: joinByProperty($scope.usersToAdd, 'personID', ',') },
{ processesToAdd: joinByProperty($scope.processesToAdd, 'processID', ',') },
{ branchesToAdd: joinByProperty($scope.branchesToAdd, 'branchID', ',') });
indicatorRepository.save(objectToSave)
.then(function () {
location.href = "/Indicator/Indicators";
}, function (errors) {
$scope.indicatorErros = errors;
validationFields();
})
};
}]);
}());
console.log ($ scope.indicator.indicatorGoals);
(2) [{...}, {...}] 0: {goalType: 1, above: 32, startGoal: "2017-10-21T00: 00: 00", endGoal: 1508378400000, $$ hashKey: "00E"} 1: {goalType: 1, above: 32, startGoal: "2017-10-20T00: 00: 00", endGoal: null, $$ hashKey: "00K"} length: 2 proto : Array (0)