AngularJS UI-router 如何将状态视图加载到顶层状态的另一个嵌套视图中?

2022-01-21 00:00:00 angularjs javascript angular-ui-router

我有一个名为 container 的顶级状态,它包含命名视图 dashboardfeed.

dashboard 视图模板里面我有一个 <div ui-view="tickers"></div> 我想加载tickers 状态,我该怎么做?

I have a top level state called container which holds the named views dashboard and feed.

这是通过在命名视图 dashboard@container 中添加一些东西来完成的吗?

Inside of the dashboard view template I have a <div ui-view="tickers"></div> in there I want to load the tickers state, how would I do that?

容器模板



The Container template

<div class="fl w100"><em>容器状态</em></div><div ui-view="dashboard" class="fl"></div><div ui-view="feed" class="fl"></div></div>

<div> <div class="fl w100"> <em>Container state</em> </div> <div ui-view="dashboard" class="fl"></div> <div ui-view="feed" class="fl"></div> </div>

仪表板模板

<div class="dashboard-state">
  <em>Dashbaord state</em>
  <div ui-view="tickers"></div>
</div>

代码模块

// Tickers module
var tickers = angular.module('tickers', ['ui.router'])

tickers.config(function($stateProvider) {

  const tickers = {
    parent: 'dashboard',
    name: 'tickers',
    url: '/tickers',
    params: {
      ticker: {}
    },
    views: {
      '': {
        templateUrl: 'tickers-template.html',
        controller: function($scope, $state) {
          console.log('TICKERS view $state');

          $scope.tickers = [
            { id: 1, ticker: 'AAPL' },
            { id: 2, ticker: 'GOOG' },
            { id: 3, ticker: 'TWTR' }
          ];

          $scope.clickTicker = function(ticker) {
            console.log('ticker', ticker)
            $state.go('tags', { ticker: ticker });
          }
        }
      },
      'tags@tickers': {
        templateUrl: 'tags-template.html',
        controller: function($scope) {
          console.log('TAGS view $state');
        }
      }
    }
  }

  $stateProvider.state(tickers);
})

ticksApp 主模块

// TickersApp module
////////////////////////////////////////////////////////////////////////////////
var tickersApp = angular.module('tickersApp', ['ui.router', 'container', 'tickers']);

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

  $urlRouterProvider.otherwise('/login');

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

  $stateProvider
    .state(login);
});

推荐答案

仍在等待更多答案,但到目前为止,我通过在 dashboard 中使用 tickers.component 得到了这个工作-template.html

Still waiting on more answers, but I got this working so far by using a tickers.component inside of the dashboard-template.html

https://plnkr.co/edit/6dLIk1vq6M1Dsgy8Y4Zk?p=preview

<div class="dashboard-state">
  <div class="fl w100">
    <em>Dashbaord state</em>  
  </div>

  <!--<div ui-view="tickers"></div>-->
  <tickers-module></tickers-module>
</div>

<小时>

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

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

相关文章