Version: 1.5.5
I have two arrays in my scope, let's call them:
$scope.array1 = [1,2,3,4];
$scope.array2 = [5,6,7,8];
I want to modify one of this arrays based on a condition, for this I want to use the ternary conditional because I'll make changes on the array in different parts of my code, thus I'll avoid repeating the condition multiple times:
var header = (myCondition)?$scope.array1:$scope.array2;
header = [];
header.push(10);
//some code
header.push(11);
But this is not working! The changes on header are not reflected on the object in the $scope.
So I have to do this:
if(myCondition){
$scope.array1 = [];
$scope.array1.push(10);
}
else{
$scope.array2 = [];
$scope.array2.push(10);
}
//some code
if(myCondition){
$scope.array1.push(11);
}
else{
$scope.array2.push(11);
}
Which seems awful...
I think the first way should really work because header will keep the reference to the object in the $scope. Anyway, that was the dummy code to make my point, here is the real code (sorry if it's awful. My first time with Javascript):
$scope.getHeaders = function(type){
req = createJsonedFilters(false, true, true);
req['type'] = type;
$http.post("/getHeaders", req).then(
function(res){
data = res.data;
if(data['msg'] == "OK"){
var minmax = (type == 'epidem')?"epidemMinMax":"entomoMinMax";
var headers = (type == 'epidem')?$scope.epidemHeaders:$scope.entomoHeaders;
headers = [];
data_minmax = data[minmax]
minmax_headers = Object.keys(data_minmax).sort();
console.log(minmax_headers);
for(var i=0; i < minmax_headers.length; i++){
var minmax_name = minmax_headers[i];
var mm_elem = data_minmax[minmax_name]
var atributo = {
name: minmax_name,
enabled: true,
slider: {minValue: parseInt(mm_elem['min']), maxValue: parseInt(mm_elem['max']),
options: {floor: parseInt(mm_elem['min']),ceil: parseInt(mm_elem['max']),
step: 1}}
};
headers.push(atributo);
}
console.log(headers);
$scope.refreshAttributes();
}
else{
console.log('Empty ' + type + ' Dataset...')
}
},
function(){
console.log("Couldn't load " + type + " headers...")
}
);
}
Any ideas why the reference to headers won't modify the object in the scope? If there is any output I can give you, comment and I'll update the question.