Search code examples
angularjsangular-ui-routerangularjs-http

The ui router resolve result data is not injected in the controller


In my SchoolyearController the parameter schoolyears is undefined.

How can I retrieve my schoolyears objects in the schoolyearService and inject the result into the SchoolyearController?

SERVICE

'use strict';
angular.module('schoolyear').service('schoolyearService', function ($http) {

    return {
        getSchoolyears: function () {            
            var path = 'scripts/model/schoolyears.json';
            $http.get(path).then(function (response) {
                return response.data.schoolyears;  // The schoolyears here are the 6 expected JS objects in an array, so far so good but how do I get those objects into the SchoolyearController as parameter ?
            });
        }
    };
});

UI-ROUTER

resolve: {
    schoolyearService: ['schoolyearService',
        function (schoolyearService) {
            return schoolyearService.getSchoolyears();
        }]
},

CONTROLLER

'use strict';
angular.module('schoolyear').controller('SchoolyearController', function ($scope, schoolyears) {

    $scope.schoolyears = schoolyears; // I do not want to do a $http request here I just want to get passed the data here !!!
});

UPDATE

Still the schoolyears in the resolved property are undefined, why?

FACTORY

'use strict';
angular.module('schoolyearModule').factory('schoolyearFactory', function ($http) {

    return {
        getSchoolyears: function () {
            var path = 'scripts/model/schoolyears.json';
            $http.get(path).then(function (response) {
                return response.data.schoolyears;  // The schoolyears here are the 6 expected JS objects in an array
            });
        }
    };
});

UI-ROUTER

resolve: {
    schoolyears: function(schoolyearFactory) {
        var schoolyears = schoolyearFactory.getSchoolyears();
        return schoolyears;
    }

},

CONTROLLER

'use strict';
angular.module('schoolyearModule').controller('ProjectsController', function ($scope, schoolyears) {

    $scope.schoolyears = schoolyears; // I do not want to do a $http request here I just want to get passed the data here !!!
});

Solution

  • Your resolved value is named schoolyearService (and thus clashes with the service which has the same name):

    resolve: {
        schoolyearService: ...
    

    But you're trying to inject it using the name schoolyears:

    angular.module('schoolyear').controller('SchoolyearController', 
        function ($scope, schoolyears) {
    

    Use the same name (schoolyears) everywhere:

    resolve: {
        schoolyears: ...
    

    Also, you should use the factory() method to define your service, and not the servoce() method. The service() method takes a constructor function as argument, not a function returning an object being the actual service instance.

    EDIT:

    Moreover, you're not returning anything from the getSchoolyears() service method. So undefined is returned. What you need is:

        getSchoolyears: function () {
            var path = 'scripts/model/schoolyears.json';
            return $http.get(path).then(function (response) {
                return response.data.schoolyears;
            });
        }