【问题标题】:How to send object from 1 $state into another $state with ui-router如何使用 ui-router 将对象从 1 $state 发送到另一个 $state
【发布时间】:2017-03-17 16:47:01
【问题描述】:

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

预期

Login 之后选择一个代码按钮应该使标签模块显示该代码的匹配标签。

结果

Login 之后,选择一个 Ticker 按钮会将索引的 ui-view 中的整个应用程序替换为 Tags $state 对象。



App当前状态路径:-> login > container > tags

Tickers 组件中的ticker 按钮点击:

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

【问题讨论】:

    标签: javascript angularjs routing angular-ui-router


    【解决方案1】:

    有更新的plunker

    也就是说,我们必须将 tags 状态设置为 container one 的子级

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

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

    <div ui-view>
    </div>
    

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

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

    查看here 并进一步了解嵌套,也许可以查看:

    【讨论】:

    • 谢谢!我现在看到的唯一问题是,最初我想知道如何默认启动 tagsModule / 组件,但是在 Tickers 中我可以调用 $state.go('tags', { ticker: $scope.tickers[0] }); 并继续发送默认标签列表:)
    • 嗯,在下一步又卡住了,介意再快速浏览一下和更多的互联网点吗? :D stackoverflow.com/questions/42865321/…
    【解决方案2】:

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

    您应该查看如何进行嵌套视图/状态: https://github.com/angular-ui/ui-router/wiki/Nested-States-&-Nested-Views

    【讨论】:

    • 好吧,我分离出这样的路由配置的原因是我试图控制哪些组件控制器刷新。如果没有,我不希望每个模块都重新初始化。在我的情况下,Feed 应该只初始化一次,并且没有其他组件应该影响它。但是,一旦您在 Feed 中执行操作,它就会刷新并影响其他人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2013-09-05
    • 1970-01-01
    • 2020-10-25
    • 2016-12-09
    • 1970-01-01
    • 2011-01-09
    相关资源
    最近更新 更多