繁体   English   中英

如何使用ui-router将对象从1 $状态发送到另一个$ state

[英]How to send object from 1 $state into another $state with ui-router

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

预期

登录后选择Ticker按钮应使标签模块显示该Ticker的匹配标签。

结果

登录后选择Ticker按钮将使用Tags $ state对象替换索引的ui-view中的整个应用程序。


在此输入图像描述


在此输入图像描述

应用程序的当前状态路径: - > login > container > tags

单击代码组件中的自动收报机按钮:

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

app.container.html(我们登录后去这里) 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>

完整代码

// 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);
  }
})

有更新的plunker

也就是说,我们必须将标签状态设置为container 1的子级

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

另外,现在模板中的容器状态有一个目标

<div ui-view>
</div>

最后 - 标签stat将标签模块作为其模板的一部分

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

在这里查看 ,也可以更好地了解嵌套,也许检查一下:

您需要在一个模块中拥有所有路由配置,否则不同的路由(状态)将不会相互了解。

你应该看看如何做嵌套的视图/状态: https//github.com/angular-ui/ui-router/wiki/Nested-States-&-Nested-Views

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM