gpt4 book ai didi

angularjs - ui-router 默认子状态不起作用

转载 作者:行者123 更新时间:2023-12-04 17:18:15 26 4
gpt4 key购买 nike

我正在测试 UI-Router嵌套状态,但我无法在父/子场景中设置默认状态,请帮助。

图书馆:

  • Angular 1.3.15
  • ui 路由器:0.2.15

  • 导航路径:
    / - home
    /settings - parent state/page
    /settings.default - child 1
    /settings.mail - child 2
    /settings.phone - child 3

    预期行为:

    导航到 /settings 时,应该触发/加载默认的子“常规”状态/页面

    实际行为:

    如果我设置 'settings' state 'abstrat: true',就会出现错误:
    Error: Cannot transition to abstract state 'settings'
    at Object.transitionTo (angular-ui-router.js:3143)
    at Object.go (angular-ui-router.js:3068)
    at angular-ui-router.js:4181
    at angular.js:16299
    at completeOutstandingRequest (angular.js:4924)
    at angular.js:5312

    如果我删除 'abstrat: true',则没有错误,但是当我导航到/settings 时,默认子状态/页面 - 一般状态未触发,settings.html 显示但未加载

    app.js:
    angular.module('test',['ui.router','sails.io'])     //config routing
    .config(['$stateProvider','$urlRouterProvider','$locationProvider',
    function($stateProvider,$urlRouterProvider,$locationProvider) {

    $urlRouterProvider.otherwise('/');
    $locationProvider.html5Mode(true);

    $stateProvider
    .state('home', {
    url: '/',
    templateUrl: 'views/index.html',
    controller: 'IndexController'
    })
    .state('settings', {
    abstract: true,
    url: '/settings',
    templateUrl: 'views/settings.html',
    controller: 'SettingsController'
    })
    .state('settings.general', {
    url: '',
    templateUrl: 'views/settings/general.html',
    controller: 'SettingsController'
    })
    .state('settings.mail', {
    url: '/mail',
    templateUrl: 'views/settings/mail.html',
    controller: 'SettingsController'
    })
    .state('settings.phone', {
    url: '/phone',
    templateUrl: 'views/settings/phone.html',
    controller: 'SettingsController'
    });
    }]);

    View /settings.html
    <div class="container">
    <div class="row">
    <div class="col-md-3">
    <ul class="list-group">
    <li class="list-group-item"><a ui-sref=".general">General</a></li>
    <li class="list-group-item"><a ui-sref=".phone">Phone</a></li>
    <li class="list-group-item"><a ui-sref=".mail">Mail</a></li>
    </ul>
    </div>
    <div class="col-md-9">
    <div ui-view></div>
    </div>
    </div>
    </div>

    最佳答案

    a working plunker

    这会起作用,但仅适用于 url导航,即 href :

      <a href="/settings"> // will work

    但这不会 - 我们不能去 abstract 状态:
      <a ui-sref="settings">settings</a>

    但基于这个问答:

    Redirect a state to default substate with UI-Router in AngularJS

    我们可以像这样删除抽象并创建默认状态处理。有 updated plunker
    // redirection engine
    app.run(['$rootScope', '$state', function($rootScope, $state) {

    $rootScope.$on('$stateChangeStart', function(evt, to, params) {
    if (to.redirectTo) {
    evt.preventDefault();
    $state.go(to.redirectTo, params)
    }
    });
    }]);

    父级统计调整:
    .state('settings', {
    //abstract: true,
    redirectTo: 'settings.general',
    url: '/settings',
    ...

    查一下 here

    关于angularjs - ui-router 默认子状态不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30347359/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com