Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - How to pass up to date $routeParams to resolve?

How do you pass current $routeParams to resolve so they can be used to do a specific $http.get. Right now I've just tested $routeParams and passed back the param to be logged in my controller to see if it works, but it is a route behind each time. For example, on load it is undefined, and on subsequent routes the param is the previous routes param, so I click a new link and it shows the param from the previous link's route.

Route Snippet

    .when('/something/:paramType', {
        templateUrl: '/app/views/something.html',
        controller: 'SomethingController',
        controllerAs: 'someCtrl',
        access: {
            authentication: true
        },
        resolve: {
            SomeData: function( $routeParams ) {
                return $routeParams.paramType;
            }
        }
    })

Controller Snippet

   .controller('SomethingController', ['$scope', 'SomeData', function( $scope, SomeData) { 

       console.log(SomeData);
   }])

How do you initialize the param on load, and have it sync correctly each time? The reason I want to do this is so I can perform an $http.get and pass the $routeParams to define the returned data.

like image 584
mtpultz Avatar asked Sep 16 '14 21:09

mtpultz


1 Answers

From the angular documentation for $routeProvider

Be aware that ngRoute.$routeParams will still refer to the previous route within these resolve functions. Use $route.current.params to access the new route parameters, instead.

So..

SomeData: function( $route ) {
    return $route.current.params.paramType;
}
like image 61
David Beech Avatar answered Nov 06 '22 04:11

David Beech