1

I have a router that's waiting for these properties to be resolved

$stateProvider.state('friends', {
            url: '/friends/{id}',
            templateUrl: '/friends.html',
            controller: 'friendsCtrl',
            resolve: {
                friend: ['$stateParams', 'friends', function($stateParams, friends){
                    return friends.get($stateParams.id);
                }],
                notes: ['$stateParams', 'friends', function($stateParams, friends){
                    return friends.getNotes($stateParams.id);
                }]
            }
        });

This is friends.get and friends.getNotes.

friends.get = function(id){
        return $http.get('/friends/' + id).then(function(res){
            return res.data;
        });
    };

    friends.getNotes = function(id){
        return $http.get('/friends/' + id + '/notes').then(function(res){
            console.log(res.data);
            return res.data;
        });
    }; etc... (10 more methods)

friends is an object with approximately 12 different methods, getNotes and get just being two of them. I've checked the console log for getNotes and get. get returns a friend object (as I want), and getNotes returns an array of notes (also as I wanted).

This issue comes inside the controller. friend gets resolved to a friend object as I want. HOWEVER, notes gets resolved as the entire friends object (it's not an array, it happens to literally be the friends object with methods like get and getNotes).

This is how I inject friend and notes into the controller

app.controller('friendsCtrl', ['$scope', 'friend', 'notes', 'friends',
    function($scope, friend, friends, notes){
        $scope.doComment = false;
        $scope.friend = friend;
        console.log(friend);
        $scope.notes = notes;
        console.log(notes);

Logging friend gives me a friend object. Logging notes doesn't give me the array of notes as expected, rather I get the ENTIRE friends object. So instead of [note1, note2, note3] I get Object with methods getNotes, get, etc. Any ideas why I'm getting the friends object rather than the notes array in notes?

1 Answer 1

2

The order of injected services is incorrect. Should be

function($scope, friend, notes, friends)

according to ['$scope', 'friend', 'notes', 'friends', function() {...}.

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

2 Comments

Wow lifesaver.. Didn't realize that was required ordering. I'll accept the answer after the time limit's up. Thanks!!
Yes, the whole point of this array notation is to specify order of services. Services will be injected according to the order of strings, so after minification when function arguments becomes function(a, b, c, d) {} Angular could still figure out that a is $scope, b is firend, etc.

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.