How to pass $state variable into 2 other sibling $states?

大兔子大兔子 提交于 2019-12-01 06:45:19

I've updated your plunker

https://plnkr.co/edit/ywyH7wOmR6loNiAkH9Yb?p=preview

The solution is fairly simple. Instead of using $state use $stateParams in the dependency injection which is a singleton so if you reference it like I did in the updated plunker:

$scope.params = $stateParams

then the display value

{{params.ticker.ticker}}

will be 2 way bound to the $stateParams and update accordingly every time you change state

---edit---

I've added another solution through service https://plnkr.co/edit/oQNAHv7tAS8LR9njOUAX?p=preview

.service('awesomeTag', function($rootScope){
  var self = this;
  $rootScope.$on('$stateChangeSuccess', function(_event, _toState, toParams){
    self.tag = toParams.ticker.ticker
  })

})
clever_bassi

I updated your plunker here. The issue in your code was here:

view.component('viewModule', {
  templateUrl: 'view-module-template.html',
  controller: function($scope, $state) {
    console.log('View component', $state.params);
    $scope.term = $state.params.tag.term; //culprit line
  }
});

What you need to instead do is use $stateParams to retrieve parameters. I changed the code in viewModule as follows:

controller: function($scope, $stateParams) {
    console.log('View component', $stateParams);
    $scope.tickerObj = $stateParams;     
}

And then accordingly updated the view-module-template.html to use the tickerObj as follows:

{{ tickerObj.ticker.ticker }}

Same thing for social-module as well.

A simple example to understand how this works is as follows:

$stateProvider.state('stateName', {
    url: '/stateName',
    controller: 'myCtrl',
    params: { //this can also be used to give default values for route params
        obj: null
    }
});

function myCtrl($stateParams) {
    console.log($stateParams);   //will display the obj parameter passed while transitioning to this state
}

$state.go('stateName', {obj:yourObj}); //passing obj as param to stateName state, this can be retrieved using $stateParams. 

This is similar to the issue happening in your last question.

I noticed another issue with your code. On clicking any of the tags like retweet,moments,tweet, it changes the ticker to "AAPL" again. Is this intended behavior? (If not, this is happening because when you transition to dashboard state after clicking on a ticker, all modules are being re-initialized. The line causing this is

$state.go('tags', { ticker: $scope.tickers[0] });

Hope this helps!

Update:

I updated the plunker here. I commented out this line:

$state.go('tags', { ticker: $scope.tickers[0] }); 

which was by default setting ticker to apple each time we were coming to dashboard state. I am also passing ticker and tag object now to both view and social state. If you look at the console, you're able to get the correct values to tag and tickers to both those states. Let me know if this works for you.

You can verify that the tag and ticker values are being passed into the view and social components by looking at the console logs. Here is a snippet:

Posting this because this is the correct way to resolve this issue, and properly architect the app:

https://plnkr.co/edit/MztpsHj9qoRCFUDrREH7?p=preview

Here I'm correctly using child-states and named views.

container-template.html

<div>
  <div class="fl w100">
      <em>Container component module scope</em>  
  </div>

  <div ui-view="dashboard"></div>

  <div ui-view="feed" class="fl"></div>
</div>

The tags state

const tags = {
  name: 'container.dashboard.tickers.tags',
  url: '/tags',
  params: {
    ticker: {},
    tag: {}
  },
  views: {
    'tags' : {
      templateUrl: 'tags-list.html',
      controller: function($scope, $state) {
        console.log('tags-list controller', $state)
        $scope.ticker = $state.params.ticker;

And state navigation to child.child states:

$scope.clickTicker = function(ticker) {
    $state.go('container.dashboard.tickers.tags', { ticker: ticker });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!