1

I have two states as follows:

  .config(function($stateProvider, $urlRouterProvider) {
      $stateProvider
        .state('my.list', {
          url: '/myitem',
          templateUrl: 'templates/my-item-list.html',
          controller: 'myController'
        })

        .state('my.detail', {
          url: '/detail',
          templateUrl: 'templates/item-detail.html',
          controller: 'myController'
        })

In my-item-list.html I have used ng-repeat to list items, which are clickable.

On click the state changes to my.detail where I want to display details of selected/clicked item.

How to pass clicked data from one state to another? What would be best approach to follow? without displaying params in URL?

1 Answer 1

1

Best approach while navigating from one state to another on click of a link is by using ui-sref and you can pass params with it like:

<a ... ui-sref="my.detail({selected: 'something'})" ...>...</a>

In order to make that work, you need selected to be part of params in that state. Like this:

    .state('my.detail', {
      url: '/detail',
      params: {selected: null},
      templateUrl: 'templates/item-detail.html',
      controller: 'myController'
    })

Now, you can use that in your myController using $stateParams.selected.

Sign up to request clarification or add additional context in comments.

6 Comments

will it be same using $state.go('my.detail');
ui-sref can be used in HTML like <a ... ui-sref="my.detail({...})"..>..</a>.. if you have direct navigation, you should use ui-sref, if you have any logic written in controller and then based on that you are changing states, then you should use $state.go('my.detail', {...}) in controller
@SystemMGR you can find more on ui-sref here. It's very simple to use.
This solution looks easy but I am using JSON data to render in list (ng-repeat), how can I pass this data to details state?
@Thanos ask your question seperately mentioning the issues you have and I could help you out
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.