gpt4 book ai didi

javascript - 如何使用 UI-Router 仅更新命名 View

转载 作者:行者123 更新时间:2023-11-29 10:38:53 24 4
gpt4 key购买 nike

我正在创建一个网络应用程序来帮助学生学习科学、历史和数学。当您第一次登陆该网站时,我有一个主页/登陆页面。当您单击“开始”时,我会转到/exam/instructions。我的每一步说明、数学和科学都是我加载到 ui-view="exam-detail" 中的模板。目前,当我通过科学导航到指令和从指令导航时,整个 ui-view 加载。理想情况下,我只需要一个分页区域和一个主题区域,并且只希望 ui-view="exam-detail" 使用正确的模板进行更新。

我根本没有使用过 UI-Router,非常感谢任何帮助。

index.html

<div ui-view></div>

国家考试>exam.html

<div class="state-exam">
<nav ui-view="exam-pagination"></nav>
<section ui-view="exam-detail"></section>
</div>

路由.js

(function() {
'use strict';

angular
.module('studentPortal')
.config(routeConfig);

function routeConfig($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/',
templateUrl: 'app/main/main.html',
controller: 'MainController',
controllerAs: 'main'
})

.state('exam', {
url: '/exam/:step',
abstract: true,
templateUrl: 'app/state-exam/exam.html',
controller: 'ExamController',
controllerAs: 'examController',
})

.state('exam.instructions', {
url: '/instructions',
views: {
'exam-pagination':{
templateUrl: 'app/state-exam/exam-pagination.html'
},
'exam-detail' : {
templateUrl: 'app/state-exam/exam-instructions.html'
}
}
})

.state('exam.math', {
url: '/math',
views: {
'exam-pagination':{
templateUrl: 'app/state-exam/exam-pagination.html'
},
'exam-detail' : {
templateUrl: 'app/state-exam/exam-math.html'
}
}
});
$urlRouterProvider.otherwise('/');
}

})();

最佳答案

a working plunker

其实有一个类似的问答,with working plunker:

Angular UI Router - Nested States with multiple layouts

此处的解决方案是将静态 View 从 subview 移动到父 View 。它不会为每个 child 重新加载(只有当父状态改变时 View 才会重新加载)。我们将使用绝对 命名(有关详细信息,请参阅包含的链接)

所以这是代码调整

.state('exam', {
url: '/exam/:step',
abstract: true,
// the root view and the static pagination view
// will be defined here, so we need views : {}
views: {
'':{
templateUrl: 'app/state-exam/exam.html',
controller: 'ExamController',
controllerAs: 'examController',
},
// absolute naming targets the view defined above
'exam-pagination@exam':{
templateUrl: 'app/state-exam/exam-pagination.html'
},
}
})

.state('exam.instructions', {
url: '/instructions',
views: {
// 'exam-pagination':{}, // defined in parent
'exam-detail' : {
templateUrl: 'app/state-exam/exam-instructions.html'
}
}
})

.state('exam.math', {
url: '/math',
views: {
// 'exam-pagination':{}, // defined in parent
'exam-detail' : {
templateUrl: 'app/state-exam/exam-math.html'
}
}
});

同时检查这个以获取有关绝对 View 命名的更多详细信息

working example is here

关于javascript - 如何使用 UI-Router 仅更新命名 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32448285/

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