Extending parent state with extra views

2019-05-12 05:15发布

问题:

Using: AngularJS and UI-Router.

I am trying to create a page that has two views: menu and main. One is for a menu and the other is for whatever content there may be. However, I do not want to define both views in all states every time. The menu view will not change too often. So I created a parent 'root' state which contains only the menu view. The other states then derive from this and add appropriate views.

The code looks like this (in a file app.js):

angular
    .module('Admin', ["ui.router"])
    .config(["$stateProvider", function($stateProvider) {
        $stateProvider.state('root', {
            abstract: true,
            views: {
                "menu": {
                    templateUrl: "../static/partials/menu.html",
                    controller: "MenuController"
                }
            }
        }).state('root.main', {
            url: "",
            parent: 'root',
            views: {
                "main": {
                    templateUrl: "../static/partials/landing.html",
                    controller: "MainController"
                }
            }
        }).state('root.login', {
            url: "/login",
            parent: 'root',
            views: {
                "main": {
                    templateUrl: "../static/partials/login.html",
                    controller: "LoginController"
                }
            }
        })
        ;
    }])
    .controller('MainController', ["$scope", "$http", mainController])
    .controller('MenuController', ["$scope", "$http", menuController])
    .controller('LoginController', ["$scope", "$http", loginController])
    ;

The result is that only the menu view is displayed. The main view is not displayed, unless I also add it to the root state. Anyone know what is wrong?

EDIT

The HTML that contains the views:

<div ng-app="Admin">
    <a ui-sref="root.main">Click me</a>
    <div class="ui menu" ui-view="menu"></div>
    <div class="ui segment">
        <div ui-view="main"></div>
    </div>
    <br>
</div>

<script src="{{ url_for('static', filename='js/menu.js') }}"></script>
<script src="{{ url_for('static', filename='js/main_controllers.js') }}"></script>
<script src="{{ url_for('static', filename='js/app.js') }}"></script>

EDIT 2

There is a similar question here: UI-Router inheriting views, but this does not work for me...

EDIT 3

I managed to very simply reproduce this in plnkr: http://plnkr.co/edit/uYlFgsvq8hQHON8EESEx?p=preview

回答1:

Just had the same problem and found the solution using your plnkr. You need to add the '@' to the view name as shown below.

.state('root.main', {
    url: "",
    parent: 'root',
    views: {
        "main@": {
            templateUrl: "../static/partials/landing.html",
            controller: "MainController"
        }
    }
})


回答2:

Your whole code is right only you need to call that route from UI by doing ui-sref="root.main" or ui-sref=".main"

Markup

<div ng-app="Admin">
    <a ui-sref="root.main">Click me</a>
    <div class="ui menu" ui-view="menu"></div>
    <div class="ui segment">
        <div ui-view="main"></div>
    </div>
    <br>
</div>