angularjs - AngularJs ui-router onEnter 在视图加载之前触发
问题描述
我有几个嵌套视图。Html如下。对于索引页面:
<section ui-view id="one"></section>
然后我有第二个嵌套视图,如下所示:
<!--- Removed for brevity --->
<section ui-view="two" id="two"></section>
最后我有另一个嵌套视图,如下所示:
<!--- Removed for brevity --->
<section ui-view="three" id="three"></section>
所以,路线;它们看起来像这样:
$stateProvider
.state('home.one', configureOneRoute());
function configureOneRoute() {
return {
url: ':categoryName/one',
views: {
'@': {
templateUrl: 'scripts/one/one.html',
controller: 'StepOneController',
controllerAs: 'controller'
}
},
params: {
clear: false
},
resolve: {
options: getOptions,
category: getCategory,
criteria: getCriteria,
groups: getGroups,
priorityColours: getPriorityColours,
products: getProducts,
questions: getQuestions,
settings: getSettings,
createSession: createSession,
applyQuestions: applyQuestions,
updateSession: updateSession
},
data: {
pageTitle: 'Step one'
},
onEnter: function (products) {
console.log(products);
$('html, body').animate({
scrollTop: $('#two').offset().top
}, 2000);
}
};
};
然后我有我的第二条路线:
$stateProvider
.state('home.one.two', configureTwoRoute());
function configureTwoRoute() {
return {
url: '/two',
views: {
'two': {
templateUrl: 'scripts/two/two.html',
controller: 'StepTwoController',
controllerAs: 'controller'
}
},
resolve: {
updateSession: updateSession
},
data: {
requireLogin: true,
pageTitle: 'Step two'
},
onEnter: function (products) {
console.log(products);
$('html, body').animate({
scrollTop: $('#two').offset().top
}, 2000);
}
};
};
最后是我的第三个观点:
$stateProvider
.state('home.one.two.three', configureThreeRoute());
function configureThreeRoute() {
return {
url: '/three',
views: {
'three': {
templateUrl: 'scripts/three/three.html',
controller: 'StepThreeController',
controllerAs: 'controller'
}
},
resolve: {
updateSession: updateSession
},
data: {
requireLogin: true,
pageTitle: 'Step three'
},
onEnter: function (products) {
console.log(products);
$('html, body').animate({
scrollTop: $('#three').offset().top
}, 2000);
}
};
};
如果我导航到每个视图,它工作正常。但如果我在第二步并刷新页面,我会收到此错误:
无法读取未定义的属性“顶部”
我知道这是因为第一个视图还没有加载,但决心已经加载。因此,ui-view
for“two”不存在,因此您无法滚动到它。
有没有办法解决这个问题?
解决方案
听起来像是刷新时的竞争条件。尝试将该代码置于超时状态或将其置于映射控制器中。
从文档看来,它不能保证页面会被呈现,只是状态是活动的:
推荐阅读
- python - 将本地 python 文件作为类导入
- node.js - 使用 Node.js 复制文件夹的正确方法是什么
- prolog - 排除解决方案中列表的变体/轮换 SWI-Prolog
- laravel - Laravel 用另一个模型更新模型
- spring - JPA Repository - 在列表中获取由表连接引起的重复项
- typescript - 在 TypeScript 类中获取方法名称
- travis-ci - 将 TravisCI 设置为仅在主服务器上推送时发布包
- artificial-intelligence - CLIPS - 是否可以在运行时更改规则的显着性?
- python - 如何使用 postgres 在 peewee 查询中访问 ArrayField 的最后一个索引?
- java - 我无法通过添加数组中每个元素的特定值来计算