cjskywalker
cjskywalker

Reputation: 3285

Access scope variable from the other controller within the nested state

How to access other state scope variable from another state ctrl? If in 'session.index.detail' state, I want to access leftPanel scope variable from rightPanel Ctrl is it possible? What I want to do is I have a form in rightPanel, when I click save, I want to add the new data object back to leftPanel list and refresh it after it is successfully added to the database.

.state('session', {

      abstract: true,
      templateUrl: '/schedule/_session.html'
    })
    .state('session.index', {
      url: '/sessionmang',
      views: {
        'leftPanel@session': {
          controller: 'SessionCtrl',
          controllerAs: 'vm',
          templateUrl: '/schedule/_sessionPanel.html'
        },
        'rightPanel@session': {
          templateUrl: '/schedule/_sessionDetailPanel.html'
        }
      }
    })
    .state('session.index.detail', {
      params: {
        sessionObj: null,
        sessionTypeObj: null
      },
      views: {
        'rightPanel@session': {
          controller: 'SessionDetailCtrl',
          controllerAs: 'vm',
          templateUrl: '/schedule/_sessionDetailPanel.html',
          resolve: {
            sessionObj: ['$stateParams', function ($stateParams) {
              return $stateParams.sessionObj;
            }],
            sessionTypeObj: ['$stateParams', function ($stateParams) {
              return $stateParams.sessionTypeObj;
            }]
          }
        }
      }
    });

Upvotes: 1

Views: 291

Answers (2)

miqe
miqe

Reputation: 3369

the simplest way is, you could use events like this.

when you click save, you will do $scope.$broadcast('myevent', data); from the rightCtrl and from the leftCtrl you could do

$scope.$on('myevent', function(event,data) {
   //do your updates here
})

Upvotes: 0

Bob Thomas
Bob Thomas

Reputation: 282

I think the best way is to create a service that holds the data you want to share.

And share it between the controllers. Or use broadcasting to talk between the controllers and pass data over that way

Upvotes: 3

Related Questions