【问题标题】:AngularJS UI-router How to load a state view into another nested view of a top level state?AngularJS UI-router 如何将状态视图加载到顶层状态的另一个嵌套视图中?
【发布时间】:2017-08-14 21:01:00
【问题描述】:

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

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

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

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

容器模块

// Container module
////////////////////////////////////////////////////////////////////////////////
var container = angular.module('container', [ 'ui.router' ])
  container.config(function($stateProvider) {
    const container = {
      name: 'container',
      url: '/container',
      views: {
        '': {
          templateUrl: 'container-template.html',
          controller: function($scope) {
            console.log('CONTAINER view $state');
          }
        },
        'dashboard@container': {
          templateUrl: 'dashboard-template.html',
          controller: function($scope) {
            console.log('DASHBOARD view $state');
          }
        },
        'feed@container': {
          templateUrl: 'feed-template.html',
          controller: function($scope) {
            console.log('FEED view $state');
          }
        }
      }
    }

    $stateProvider.state(container);
  });

容器模板

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

【问题讨论】:

    标签: javascript angularjs angular-ui-router


    【解决方案1】:

    我认为你应该像这样在容器上使用不同的状态:

    $stateProvider .state('容器', { url: '/容器', 模板网址:'' })

        .state('container.feed', {
            url: '/container/feed',
            templateUrl: 'partial-home.html'
        })
    
        .state('container.dashboard', {
            url: '/container,
            templateUrl: 'partial-home.html'
        })
    

    并使用 ui-sref

    【讨论】:

    • 我想避免ui-sref 并在所有控制器中使用$state.go。好的,如果我采用上述方法,我的问题仍然存在,如何在仪表板状态中实现代码状态?你能分叉一个plnkr吗?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多