Gyonder
Gyonder

Reputation: 3756

$stateProvider params null with ionic

I can't retrieve parameters passed from ui-router in Ionic. Parameters passed into the Controller are undefined This is my state code:

.state('app.dayliston', { 
       cache: false,
       url: '/myurl', 
       views: { 
         'mainContent': { 
           templateUrl: 'calendar/daylist.html', 
           controller: 'MyCtrl',
             params : { 'mode':'online'}
         } 
       } 
     }) 

and here is My Controller code:

.controller('MyCtrl', function($scope,$state,  $stateParams,CalendarFactory,FBFactory, $ionicHistory,$ionicScrollDelegate,$ionicModal,$ionicPopup, $timeout) { 

         console.log('MyCtrl')
         console.log('mode'+$stateParams.mode) // mode is undefined

        .... 

})

I'm using 1.6.1. Is there anything wrong with my code?

Upvotes: 1

Views: 644

Answers (4)

campsjos
campsjos

Reputation: 1405

As I can see in your code, you dont need to use $stateParams because you don't get the "mode" parameter from the URL.

I think attached data in state will be a better choice (Docs):

.state('app.dayliston', { 
   cache: false,
   url: '/myurl', 
   data:{ 
     mode: 'online'
   },
   views: { 
     'mainContent': { 
       templateUrl: 'calendar/daylist.html', 
       controller: 'MyCtrl'
     } 
   } 
 }) 

Then you can get the data stored in state like this:

.controller('MyCtrl', function($scope, $state, $stateParams, CalendarFactory, FBFactory, $ionicHistory, $ionicScrollDelegate, $ionicModal, $ionicPopup, $timeout) { 
     console.log('MyCtrl')
     console.log('mode'+$state.current.data.mode) // "online"
})

Upvotes: 1

Aaron Saunders
Aaron Saunders

Reputation: 33345

you are passing the $stateParams incorrectly

https://github.com/angular-ui/ui-router/wiki/URL-Routing#stateparams-service

The should be on the url or you can pass data in using the resolve map on the state.

https://github.com/angular-ui/ui-router/wiki#resolve

also passing in custom data might be a better approach? Hard to tell from the code sample you provided

Upvotes: 0

manzapanza
manzapanza

Reputation: 6215

Route:

.state('app.dayliston', { 
  cache: false,
  url: '/myurl/:mode', 
  views: { 
    'mainContent': { 
      templateUrl: 'calendar/daylist.html', 
      controller: 'MyCtrl'
    } 
  } 
})

Check URL Routing Query Parameters doc

Link from view:

<a ui-sref="app.dayliston({mode: 'online'});">Go to dayliston</a>

Go to state/route from controller:

$state.go('app.dayliston', {mode: 'online'});

Upvotes: 0

Mike Feltman
Mike Feltman

Reputation: 5176

MyCtrl is the actual name of your controller. It's not a parameter that's passed to the controller per se.

Upvotes: 0

Related Questions