3

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

Expected

After Login Selecting a Ticker button should make the Tags module display the matching Tags for that Ticker.

Results

After Login Selecting a Ticker button will replace the entire app in the index's ui-view with the Tags $state object.


enter image description here


enter image description here

App's current state path: -> login > container > tags

The ticker button click in the Tickers component:

$scope.clickTicker = function(ticker) {
  console.log(' Ticker clicked!', ticker)
  $state.go('tags', { ticker: ticker });
}

app.container.html (We go here after login) container state

<div>
  <dashboard-module></dashboard-module>
  <feed-module></feed-module>
</div>

dashboard.html dashboard state

<div class="jumbotron text-center">
    <h1>The Dashboard</h1>
</div>

<div class="row">
  <tickers-module></tickers-module>
  <tags-module></tags-module>
  <view-module></view-module>
  <social-module></social-module>
</div>

Full Code

// Container module
////////////////////////////////////////////////////////////////////////////////
var container = angular.module('container', [ 'ui.router' ])
    container.config(function($stateProvider) {
      const container = {
        name: 'container',
        url: '/container',
        templateUrl: 'app.container.html'
      }

      $stateProvider.state(container);
    });

// Tickers module
////////////////////////////////////////////////////////////////////////////////
var tickers = angular.module('tickers', ['ui.router'])
    tickers.config(function($stateProvider) {

      const tickers = {
        name: 'tickers',
        url: '/tickers',
        params: {
          ticker: {}
        },
        // parent: 'dashboard',
        templateUrl: '<p>Tickers State</p>',
        controller: function($scope, $state) {
          console.log('Tickers state', $state.params);
        }
      }

      $stateProvider.state(tickers);
    })
    tickers.component('tickersModule', {
      templateUrl: 'tickers-module-template.html',
      controller: function($scope, $state) {
        console.log('Tickers component', $state.params);
        $scope.tickers = [
          { id: 1, ticker: 'AAPL' },
          { id: 2, ticker: 'GOOG' },
          { id: 3, ticker: 'TWTR' }
        ];

        $scope.clickTicker = function(ticker) {
          console.log(' Ticker clicked!', ticker)
          $state.go('tags', { ticker: ticker });
        }
      }
    });

// Tags module
////////////////////////////////////////////////////////////////////////////////
var tags = angular.module('tags', ['ui.router'])
    tags.config(function($stateProvider) {

      const tags = {
        name: 'tags',
        url: '/tags',
        params: {
          ticker: {}
        },
        // parent: 'dashboard',
        template: '<p>Tags State</p>',
        controller: function($scope, $state) {
          console.log('Tags state', $state.params);
        }
      }

      $stateProvider.state(tags);
    })
    tags.component('tagsModule', {
      templateUrl: 'tags-module-template.html',
      controller: function($scope, $state) {
        console.log('Tags component', $state.params);
        const tags_model = [
          {
            ticker: 'AAPL',
            tags : [{ id: 1, term: 'iPhone 7' }, { id: 2, term: 'iPhone 8' }, { id: 3, term: 'Tim Cook' }]
          },
          {
            ticker: 'GOOG',
            tags : [{ id: 4, term: 'Pixel' }, { id: 5, term: 'Pixel XL' }, { id: 6, term: 'Chrome Book' }]
          },
          {
            ticker: 'TWTR',
            tags : [{ id: 7, term: 'tweet' }, { id: 8, term: 'retweet' }, { id: 9, term: 'moments' }]
          }
        ];

        function matchTags(ticker, model) {
          return model.filter(function(obj){
            if (obj.ticker === ticker) { return obj; }
          });
        }

        $scope.tags_model = matchTags($state.params.ticker.ticker, tags_model)[0];

        $scope.clickTag = function(tag) {
          $state.go('tags', { tag: tag });
        }

        console.log('Tags init', $state.params);
      }
    });

// ViewHeader module
////////////////////////////////////////////////////////////////////////////////
var view = angular.module('view', ['ui.router'])
    view.config(function($stateProvider) {

      const view = {
        name: 'view',
        url: '/view',
        params: {
          tag: {}
        },
        template: '<p>View state</p>',
      }

      $stateProvider.state(view);

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

// Social module
////////////////////////////////////////////////////////////////////////////////
var social = angular.module('social', ['ui.router'])
    social.config(function($stateProvider) {

      const social = {
        name: 'social',
        url: '/social',
        params: {
          tag: {}
        },
        template: '<p>Social state</p>',
      }

      $stateProvider.state(social);

    })
    social.component('socialModule', {
      templateUrl: 'social-module-template.html',
      controller: function($scope, $state) {
        console.log('Social component', $state.params);
        $scope.tag = $state.params.tag;
      }
    });

// Feed module
////////////////////////////////////////////////////////////////////////////////
var feed = angular.module('feed', ['ui.router'])
    feed.config(function($stateProvider) {

      const feed = {
        name: 'feed',
        url: '/feed',
        templateUrl: '<em>Feed items go here.</em>'
      }

      $stateProvider.state(feed);
    })
    feed.component('feedModule', {
      templateUrl: 'feed-module-template.html',
      controller: function($scope, $state) {
        console.log('Feed init (only once)', $state.params);
      }
    });

// RouterApp module
////////////////////////////////////////////////////////////////////////////////
var routerApp = angular.module('routerApp', ['ui.router', 'container', 'tickers', 'tags', 'view', 'social', 'feed']);

routerApp.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/login');

    const login = {
      name: 'login',
      url: '/login',
      templateUrl: 'login.html',
      bindToController: true,
      controllerAs: 'l',
      controller: function($state) {
        this.login = function() {
          $state.go('container', {});
        }
      }
    }

    const dashboard = {
      name: 'dashboard',
      url: '/dashboard',
      params: {
        ticker: {},
        tags: {}
      },
      views: {
        '' : {
          templateUrl: 'dashboard.html',
        }
      }
    }

    $stateProvider
      .state(login)
      .state(dashboard);
})
container.component('dashboardModule', {
  templateUrl: 'dashboard.html',
  controller: function($scope, $state) {
    console.log('');
    console.log('Dashboard component', $state.params);
  }
})
Abhishek
  • 1,477
  • 1
  • 10
  • 18
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529

2 Answers2

1

There is the updated plunker

Namely we have to set a tags state as a child of container one

  const tags = {
    // here we use parent as a placeholder for our child state
    parent: 'container',
    name: 'tags',
    url: '/tags',

also, the container state now in template has a target

<div ui-view>
</div>

And finally - tags stat has as a part of its template the tags-module

template: '<p>Tags State</p><tags-module></tags-module>',

Check it here and also to get more understanding about nesting, maybe check this:

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • 1
    Thanks! The only issue I see now is, initially I was wondering how to get the tagsModule / component inited by default, but then in the Tickers I can just call `$state.go('tags', { ticker: $scope.tickers[0] });` and go ahead and send in a default tags list :) – Leon Gaban Mar 17 '17 at 18:37
  • Hmm stuck again at the next step, mind another quick look and more internet points? :D http://stackoverflow.com/questions/42865321/how-to-pass-state-variable-into-2-other-sibling-states – Leon Gaban Mar 17 '17 at 19:03
0

You need to have all route configs in one module, else the different routes (states) won't know about eachother.

You should check out how to do nested views/states: https://github.com/angular-ui/ui-router/wiki/Nested-States-&-Nested-Views

Tobias
  • 873
  • 9
  • 17
  • Well the reason I've separated out route configs like this is that I'm trying to control which Component Controllers refresh. I don't want every module re-initializing if it doesn't have too. In my case the `Feed` should only init once, and no other component should affect it. However once you take an action in the Feed, then it will refresh + affect others. – Leon Gaban Mar 17 '17 at 19:05