Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use $routeParams in resolve

I use $routeParams like this right now:

My factory function:

angular.module('fifaApp')
  .factory('FifaService', ['$http',
    function($http) {
      var sdo = {
        getTeamDetails: function(code) {
          var promise = $http.get('api/team/' + code);
          promise.success(function(data, status, headers, conf) {
            return data;
          });
          return promise;
        }
      }
      return sdo;
    }
  ]);

Controller function:

.controller('TeamDetailsCtrl',
  ['FifaService','testt',
    function(FifaService,testt) {
      var self = this;
      self.team = testt.data;
    }
]);

config:

angular.module('fifaApp', ['ngRoute'])
.config(function($routeProvider) {
.when('/login', {
      templateUrl: 'views/login.html'
    })
.when('/team/:code', {
      templateUrl: 'views/team_details.html',
      controller:'TeamDetailsCtrl as teamDetailsCtrl',
      resolve: {
          testt: ['$routeParams','UserService',
          function($routeParams, UserService) {
          return FifaService.getTeamDetails($routeParams.code);
      }]
  }
});
$routeProvider.otherwise({
      redirectTo: '/'
 });
});

But views/team_details.html does not show. Any ideas? Best Regards

like image 765
user1665355 Avatar asked Jan 21 '26 15:01

user1665355


2 Answers

I guess $routeParams doesn't work in resolve. Use $route.current.params instead:

getTeamDeatails: function() {
      var promise = $http.get('api/team');
      promise.then(function(resp){
        return resp.data;
      });
      return promise;
 }

resolve: { 
  testt: ['$route', 'FifaService', 
    function($route, FifaService ) { 
        return FifaService.getTeamDetails($route.current.params.code); 
 }]
like image 140
Ehsan88 Avatar answered Jan 24 '26 12:01

Ehsan88


Calllback function don't have an ability to return data. Your ‘getTeamDeatails‘ method should return a promise insteaf of using callbacks, for that you need to return ’$http.get ’ which already returning promise object.

 getTeamDeatails: function() {
      var promise = $http.get('api/team');
      promise.then(function(resp){
        return resp.data;
      });
      return promise;
 }

Add missing FifaService dependency in resolve method

Resolve

resolve: { 
  testt: ['$routeParams', 'FifaService', 
    function($routeParams, FifaService ) { 
        return FifaService.getTeamDetails($routeParams.code); 
 }]
like image 38
Pankaj Parkar Avatar answered Jan 24 '26 13:01

Pankaj Parkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!