How to pass $ stateParams with ui-router for service in resolution?

I have a route to receive one message and a service to request my API for this. But I need to pass the parameters from the URL to the service so that I can properly call the API. I can’t plunge into the head how to do it.

This is what I came up with. I missed what seemed to be inappropriate to this issue.

Thank you for your help!

Routing

myModule.config([ '$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) { $stateProvider .state('post', { url: '/posts/:hash_id/:slug', templateUrl: '/js/app/views/post.html', controller: 'PostCtrl', resolve: { postPromise: ['posts', function(posts, $stateParams){ //returns undefined console.log($stateParams); return posts.getOne($stateParams); }] } }) // etc 

Service

 myModule.factory('posts', ['$http', 'auth', function($http, auth){ var o = { posts: [], post: {} }; o.getOne = function(params) { // Returns undefined console.log(params); return $http.get('/api/v1/posts/' + params.hash_id).success(function(data){ angular.copy(data, o.post); }); }; return o; }]) 
+6
source share
1 answer

You missed add the $stateParams dependency in postPromise to allow

code

 postPromise: ['posts', '$stateParams', function(posts, $stateParams){ 
+11
source

Source: https://habr.com/ru/post/989759/


All Articles