0

Here is a hard one:

I have a local JSON declared in my service and I want to access part of it from my controller. Right now I can manage to access it but only to a specific section (I can't parametrize it). My intention is to access part of my JSON with a $rootScope variable or by passing an ID when I call my service, but I cant manage to make it (always returns undefined)

PS: sorry for my variables in Spanish but I make this code for me

Right now a simplified version of my code would be:

controller:

angular.module("demo").controller("myController", function($scope,$rootScope,subjectRequest) {

     $rootScope.asignaturaId = 'asignatura1';
     //$scope.asignaturaJson=subjectRequest.asignatura;//this works with my simple return (see below)
     $scope.asignaturaJson=subjectRequest.asignatura();
});

Service:

angular.module("demo").factory('subjectRequest',[ '$rootScope',function($rootScope){ 
asignaturas= {
    asignatura1:{
        id:1,
        profesorado: {
            profesor7:{ 
                title: {
                        text: 'horas Prof7'
                    },
                series: [{
                    id: 'c',
                    name: 'extra',
                    color: 'LightSkyBlue ',
                    data: [4,4,1,2,2,2,3,4,2,3,4,21]
                }, {
                    id: 'b',
                    name: 'Reservados',
                    color: 'red',
                    data: [1,1,4,1,4,3,2,1,1,5,3,1]
                }]
            },//end prof 7
            profesor8:{ 
                title: {
                        text: 'horas Prof8'
                    },                
                series: [{
                    id: 'c',
                    name: 'extra',
                    color: 'LightSkyBlue ',
                    data: [4,4,1,2,15,2,3,4,2,3,4,2]
                }, {
                    id: 'b',
                    name: 'Reservados',
                    color: 'red',
                    data: [11,1,4,1,4,3,2,1,1,5,3,1]
                }]
            }//end prof 8
        }//end profesorado          
    },//end asignatura 1

    asignatura2 : {
        id : 2,
        profesorado : {
            profesor9:{ 
                title: {
                        text: 'horas Prof4'
                    },
                series: [{
                    id: 'c',
                    name: 'extra',
                    color: 'LightSkyBlue ',
                    data: [4,4,1,2,2,2,3,4,2,3,4,21]
                }, {
                    id: 'b',
                    name: 'Reservados',
                    color: 'red',
                    data: [1,1,4,1,4,3,2,1,11,5,3,1]
                }]
            },//end prof 9

            profesor10:{ 
                title: {
                        text: 'horas Prof5'
                    },                
                series: [{
                    id: 'c',
                    name: 'extra',
                    color: 'LightSkyBlue ',
                    data: [14,4,1,2,15,2,3,4,2,3,4,2]
                }, {
                    id: 'b',
                    name: 'Reservados',
                    color: 'red',
                    data: [1,1,4,1,14,3,2,1,1,5,3,1]
                }]
            }//fin prof 10
        }//Fin Profesorado
    }//end asignatura 1
}//end asignaturas

//return { asignatura: asignaturas['asignatura1'] }; //this works

return { //I want to do something like this 
    asignatura: function(){
        return asignaturas[$rootScope.asignaturaId];
    } 

};

1 Answer 1

1

please make this line

$scope.asignaturaJson=subjectRequest.asignatura;

as

$scope.asignaturaJson=subjectRequest.asignatura();

as your service return the function which return a response too.

I hope this will help you out!

Sign up to request clarification or add additional context in comments.

1 Comment

yep, sorry I had that line for when I called the service with the call return { asignatura: asignaturas['asignatura1'] }; //this works it still doesn't work with that, but I will edit it in the question right now, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.