Javascript 角度用户界面路由器不';当页面刷新或深度链接时,不履行状态解析承诺
使用AngularJS 1.2.16、ui router和Restangular(用于API服务),我有一个抽象状态和一个使用多个视图的子状态。子视图接受URL参数(通过$stateParams)和解析,以根据通过URL传递的给定ID从API获取记录 当您在整个站点中导航时,所有这些都可以完美地工作,但是如果您在给定页面上并刷新它(Command/Ctrl+r或单击刷新按钮),或者通过点击带有ID的深层链接直接加载页面(这是更关键的问题),API服务会被请求击中,但承诺永远无法完成解决,因此,数据无法提供给州、控制器、模板等 根据ui路由器的文档,在所有承诺都得到解决之前,不应该实例化控制器。我在谷歌上下搜索过,阅读了AngularJS、Restangular和ui router文档,以及大量博客,并尝试了我知道的每一次迭代来解决这个问题,但找不到任何指向解决方案的东西 下面是有问题的代码: company.js(控制器代码) companyService.js(使用restanglar的API服务代码) 可添加的其他一些要点: *HTML5模式为true,带有哈希前缀 *Apache正在为应用程序提供重写规则集 *基本href已设置 *这是整个应用程序特有的问题,在该应用程序中,数据在某个状态下被解析;我只是选择了这个作为例子 最后,这里是控制台的一些输出:Javascript 角度用户界面路由器不';当页面刷新或深度链接时,不履行状态解析承诺,javascript,angularjs,single-page-application,angular-ui-router,restangular,Javascript,Angularjs,Single Page Application,Angular Ui Router,Restangular,使用AngularJS 1.2.16、ui router和Restangular(用于API服务),我有一个抽象状态和一个使用多个视图的子状态。子视图接受URL参数(通过$stateParams)和解析,以根据通过URL传递的给定ID从API获取记录 当您在整个站点中导航时,所有这些都可以完美地工作,但是如果您在给定页面上并刷新它(Command/Ctrl+r或单击刷新按钮),或者通过点击带有ID的深层链接直接加载页面(这是更关键的问题),API服务会被请求击中,但承诺永远无法完成解决,因此,数
In company.landing:resolve and $stateParams is: Object { id="d2c936fb78724880656008a5545b01ea445d4dc4"}
In company.landing:resolve and id is: d2c936fb78724880656008a5545b01ea445d4dc4
In CompanySvc.getCompany & id is: d2c936fb78724880656008a5545b01ea445d4dc4\
In CompanyCtrl & $state is: Object { params={...}, current={...}, $current={...}, more...}
In CompanyCtrl and company data is: undefined
In CompanyCtrl and $scope.reportData is: Object {}
In CompanyCtrl & $state is: Object { params={...}, current={...}, $current={...}, more...}
In CompanyCtrl and company data is: undefined
In CompanyCtrl and $scope.reportData is: Object {}
In CompanyCtrl & $state is: Object { params={...}, current={...}, $current={...}, more...}
Retrieved company: Object { $promise={...}, $resolved=false, route="companies", more...}
首先只是一张便条。如果您有路由问题(比如ui路由器),而不是使用小提琴,您可以使用pastebin之类的工具提供一个可以在本地运行的完整演示文件,以便测试路由问题 我用你的小提琴创作了其中一个: 有趣的是,您的模拟演示在路由方面似乎工作得非常好。这让我觉得问题出在你们的生产服务上。有一件事曾经让我对
ui路由器
感到恼火,那就是解决resolve
bug的困难,因为似乎几乎没有错误报告
如果将此项添加到应用程序运行功能中(正如我在上面的链接中所做的),您应该可以更好地解决错误输出:
// handle any route related errors (specifically used to check for hidden resolve errors)
$rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error){
console.log(error);
});
首先,您为什么要在解析定义中添加
companySvc:'companySvc'
?为什么不将CompanySvc
注入company:
resolve?嘿@MattWay。。。是的,我本来是这么做的,然后读了这篇文章,我想我应该试一试。没有帮助-/你能做一把小提琴或任何东西,复制你的问题吗?我不知道我会怎么做,因为问题只发生在硬刷新或直接点击深层链接时。我会考虑一下,看看我能做些什么。嗨@MattWay。。。我创建了一个函数来尝试重新创建这个问题,但正如您将看到的,我不认为我可以用fiddle来演示它。你不能刷新小提琴(或直接用深度链接点击唱片)。编辑:清晰。谢谢!我会深入研究的。因此,$stateChangeError即使抛出解析错误,仍然会呈现状态吗?换句话说,我最终得到的是渲染的模板,但没有数据,我希望错误能够阻止这种情况发生……是的,在这种情况下,我不确定发生了什么。如果解析失败,它不会呈现模板,如果没有$stateChangeError
,它将自动失败。没有数据的模板似乎不是路由问题,而是数据检索服务的问题。你能把一个演示挂接到真实的数据中,这样我们就可以复制这个问题了,让我们知道。当我有时间设置一个演示来实际显示问题发生的时候,我将不得不再回到这个问题上来。我本希望这只是我犯下的一些明显的错误,但并没有快乐。谢谢,马特,我会在有能力的时候更新。你找到解决方案了吗?我也有同样的疼痛
In company.landing:resolve and $stateParams is: Object { id="d2c936fb78724880656008a5545b01ea445d4dc4"}
In company.landing:resolve and id is: d2c936fb78724880656008a5545b01ea445d4dc4
In CompanySvc.getCompany & id is: d2c936fb78724880656008a5545b01ea445d4dc4\
In CompanyCtrl & $state is: Object { params={...}, current={...}, $current={...}, more...}
In CompanyCtrl and company data is: undefined
In CompanyCtrl and $scope.reportData is: Object {}
In CompanyCtrl & $state is: Object { params={...}, current={...}, $current={...}, more...}
In CompanyCtrl and company data is: undefined
In CompanyCtrl and $scope.reportData is: Object {}
In CompanyCtrl & $state is: Object { params={...}, current={...}, $current={...}, more...}
Retrieved company: Object { $promise={...}, $resolved=false, route="companies", more...}
// handle any route related errors (specifically used to check for hidden resolve errors)
$rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error){
console.log(error);
});