0

自从将 npm 更新到最新版本以来,我尝试维护 Angular 1.5.1、UI-Router 0.2.18,但似乎没有渲染任何内容。

我需要一个布局,其中顶部 ui 视图nav保留在多个页面(例如,状态)中。该页面有一个路径变量,其中包含传递给它的状态名称,即附加的 .run 函数。

代码(在 .coffee 中)

angular.module('xxx', ['ui.router'])
.config([
    '$stateProvider',
    '$urlRouterProvider',
    '$locationProvider',
    '$sceProvider',
    ($stateProvider, $urlRouterProvider, $locationProvider, $sceProvider) ->

        $sceProvider.enabled(false)

        $urlRouterProvider.otherwise('/')

        query = {
            name: 'query',
            url: '/query',
            views: {
                'nav@': {
                    templateUrl: 'nav/nav.html'
                },
                'main@query': {
                    templateUrl: 'query/query.html'
                },
                'builder@query': {
                    templateUrl: 'query/query-builder.html'
                },
                'result@query': {
                    templateUrl: 'query/query-result.html'
                }
            }
        }

        $stateProvider.state(query)

        for r in ["a", "b", "c", "d", "e"]
            query2 = {
                name: r,
                url: r,
                views: {
                    'nav': {
                        templateUrl: 'nav/nav.html'
                    }
                }
            }
            $stateProvider.state(query2)
])

.run([
    '$rootScope',
    '$state',
    '$window',
    '$timeout',
    ($rootScope, $state, $window, $timeout) ->
        path = $window.path
        if path
            $state.transitionTo(path)

])

以及相关的模板文件:

<ng-app="xxx">
<!-- Main base.html-->
    <div ui-view="nav">
<!-- Specific query page -->
    <div ui-view="main">
        <!-- Within query page, nested ui-views -->
        <div ui-view="builder"></div>
        <div ui-view="result"></div>

有什么想法吗?

4

1 回答 1

0

由于加载嵌套到组件和应用程序的视图,父组件将不会在下面的示例标题中再次加载。所以导航仍然跨越几个页面。

希望这会有所帮助。

function statesetup($stateProvider, $urlRouterProvider, $stateParams) {
    console.log('Initiallizing State StartUp...');
    $urlRouterProvider.otherwise('/');
    $stateProvider
        .state('main', {
            url: '/',
            template: 
            `
            	<header></header>
              <div ui-view>I am init </div>
            `
        })
        .state('main.a', { url: '/a',template: `<div>hi i am a </div>` })
        .state('main.b', { url: '/b',template: `<div>hi i am b </div>` })

}
function run($rootScope, $location, $state) {
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        console.log('RootState Changed...');
        var obj = { 
            event: event,
            toState: toState,
            toParams: toParams,
            fromState: fromState,
            fromParams: fromParams
        };
        
        return;
    });
}

angular.module('xxx', ['ui.router'])
    .component('header', { 
      template : `<ul>
        <li ng-click="$ctrl.nav('main.a')">a</li>
        <li ng-click="$ctrl.nav('main.b')">b</li>
       </ul>`,
       controller : function($state) { this.nav = (st) => $state.go(st); }
       })
    .config(['$stateProvider', '$urlRouterProvider', statesetup])
    .run(["$rootScope", "$location", "$state", run])
    ;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.1/angular.min.js"></script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.18/angular-ui-router.js"></script>

<div ng-app="xxx">
    <div ui-view></div>
</div>

于 2018-10-29T00:13:22.767 回答