简体   繁体   中英

Cannot transition to abstract state (angular-ui-router)

https://plnkr.co/edit/5JhQx64WF3tgdm02qKzJ?p=preview

在此处输入图片说明

All I did that broke my app was remove the ui-sref (I never like using that anyways) and went with a $state.go inside of the Javascript.

<tr ng-repeat="ticker in tickers">
  <!--<td>{{ ticker }} <button ui-sref="dash.tags({ticker: ticker})">Click Me</button></td>-->
  <td>{{ ticker }} <button ng-click="clickTicker(ticker)">Click Me</button></td>
</tr>

$scope.clickTicker = function(ticker) {
  $state.go('dash', { ticker: ticker });
}

If I don't have abstract: true in my parent dashboard state object then for some reason my TagsList component disappears.

Full code below:

var routerApp = angular.module('routerApp', ['ui.router']);

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

    $urlRouterProvider.otherwise('/dash?AAA');

    var dash = {
      name: 'dash',
      url: '/dash',
      abstract: true,
      views: {
        '': { templateUrl: 'dashboard.html' },
        'tickersList@dash': {
          templateUrl: 'tickers-list.html',
          controller: 'tickersController'
        },
        'alertsList@dash': {
          controller: 'alertsController',
          templateUrl: 'alerts-list.html',
        }
      }
    };

    var tickers = {
      name: 'dash.tickers',
      url: '?ticker',
      params: {
        ticker: 'AAA'
      },
      views: {
        'tickersList@dash': {
          templateUrl: 'tickers-list.html',
          controller: 'tickersController'
        }
      }
    }

    var tags = {
      name: 'dash.tags',
      url: '?ticker',
      params: {
        ticker: 'AAA'
      },
      views: {
        'tagsList@dash': {
          templateUrl: 'tags-list.html',
          controller: 'tagsController'
        }
      }
    }

    $stateProvider
      .state(dash)
      .state(tags)

});

routerApp.controller('tagsController', function($scope, $state, $stateParams) {
    $scope.ticker = $state.params.ticker;

    function getList(ticker) {
      switch(ticker) {
          case 'AAA' : return ['aaa tag 1', 'aaa tag 2', 'aaa tag 3'];
          case 'BBB' : return ['bbb tag 1', 'bbb tag 2', 'bbb tag 3'];
          case 'CCC' : return ['ccc tag 1', 'ccc tag 2', 'ccc tag 3'];
      } 
    }

    $scope.tags = getList($state.params.ticker);

    this.$onInit = function() {
      console.log('onInit tagsController');
    };
});

routerApp.controller('tickersController', function($scope, $state) {
    $scope.tickers = [
      'AAA', 'BBB', 'CCC'
    ];

    $scope.clickTicker = function(ticker) {
      $state.go('dash', { ticker: ticker });
    }

    this.$onInit = function() {
      console.log('onInit tickersController', $state.params.ticker);
    };
});

routerApp.controller('alertsController', function($scope, $state) {
    this.$onInit = function() {
      console.log('onInit alertsController', $state.params.ticker);
    };
});

With $state.go you go to /dash?ticker=ticker , but /dash is an abstract state, so this is why you get an error.

Your tickers and tags states have their urls defined incorrectly. You probably want something like url: 'tickers?ticker' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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