javascript - 两种状态,其中 View 定义为一种状态

标签 javascript angularjs angular-ui-router state

我正在尝试使用 angular-ui-router 模块化我的应用程序来定义具有 2 个状态的网站:主要状态和结帐状态。主要状态应该有多个“部分”标签,我试图将其定义为 ui-view 项目。我不知道我的路线设置有什么问题,但我感觉 main.html 没有被加载。关于我的定义有什么问题的任何建议...我可以避免使用部分 View 而只使用 ng-include...

routes.js

app.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/main');
    $stateProvider
        .state('main', {
            url: '/main',
            controller: 'MainCtrl',
            templateUrl: 'templates/main.html',
            views:{
                'home': {
                    templateUrl: 'templates/main.home.html'
                },
                'about': {
                    templateUrl: 'templates/main.about.html'
                },
                'services': {
                    templateUrl: 'templates/main.services.html'
                },
                'shop': {
                    templateUrl: 'templates/main.shop.html',
                    controller: 'ShopCtrl'
                }
            }
        })
        .state('checkout', {
            url: '/checkout',
            templateUrl: 'templates/checkout.html',
            controller: 'CheckoutCtrl'
});

index.html

<div ui-view id="app-ui-view"></div>

templates/main.html

<section ui-view="home" id="home"></section>
<section ui-view="about" id="about"></section>
<section ui-view="services" id="services"></section>
<section ui-view="shop" id="shop"></section>

基本上页面会加载,但主要或结帐状态不会加载。我嵌套的东西怎么错了?

最佳答案

通过不指定父级,您可以将两种状态映射到 index.html 中的默认 ui-view。因此,当访问主状态时,不会有任何模板链接到默认的 ui-view,这是现有 html 中唯一存在的模板。

所以主状态应该有这样的定义:

.state('main', {
            url: '/main',
            views:{
                '': {
                    controller: 'MainCtrl',
                    templateUrl: 'templates/main.html',
                },
                'home@main': {
                    templateUrl: 'templates/main.home.html'
                },
                'about@main': {
                    templateUrl: 'templates/main.about.html'
                },
                'services@main': {
                    templateUrl: 'templates/main.services.html'
                },
                'shop@main': {
                    templateUrl: 'templates/main.shop.html',
                    controller: 'ShopCtrl'
                }
            }
        })

关于javascript - 两种状态,其中 View 定义为一种状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39972836/

相关文章:

javascript - 代码学院错误(困惑)^^

javascript - 仅当不为 null 时才向对象添加值 - 不起作用

javascript - 按字母顺序对 Backbone 集合进行排序

javascript - 在 AngularJs 模块中包含外部工厂

MQTT 的 JavaScript 客户端不使用 WebSockets

javascript - 永远不会执行子状态 Controller

javascript - 其他 div 内的粘性页脚

javascript - Angular JS ng-click HTML 由 InnerHTML 设置,未达到作用域方法

javascript - Angularjs ui.routing - 在将 HTML 渲染到 ui-view 之前向输入元素添加属性

javascript - 错误: Could not resolve 'storysolo' from state 'index'