ChrisM February 2016

Angular - $stateparams always undefined

Im trying to use stateParams as a filter for an ng-repeat. Im trying to create user profile pages where when someone visits the url

http://localhost:3000/users/johnexample

they will see a list of johnexample's "Savings". If i visit the url now i can see the profile page fine but the param doesnt initialise.

Im trying to setup the controller variable to filter by using

$scope.usernamevalue = $stateParams.username;

Thats always undefined when i visit the url

Client Controller

angular.module('savings').controller('SavingsController', ['$scope', '$http', '$stateParams','$state' , 'Users',
    function($scope, $stateParams, $window, $state, $http, Authentication, Users) {

    $scope.usernamevalue = $stateParams.username;

]);

Route

app.route('/api/savings/of/:username')
        .get(savings.listOf);

Server controller

exports.listOf = function(req, res) { Saving.find( { user: req.params.userid }).sort('-created').exec(function(err, posts) {

    if (err) {
        return res.status(400).send({
            message: errorHandler.getErrorMessage(err)
        });
    } else {
        console.log(req.params.userid);
        res.jsonp(posts);
    }
});
};

Is there anything else that i am missing with this?

Thanks

Answers


Joe Clay February 2016

When you're injecting dependencies in Angular, the order of the function arguments needs to match the order of the strings in the array. As it stands you're injecting the $http service with the name $stateParams, hence the error.

Post Status

Asked in February 2016
Viewed 1,811 times
Voted 5
Answered 1 times

Search




Leave an answer