简体   繁体   English

为什么重定向文件下载会触发有角的ui路由器,否则会生效?

[英]why does redirect for file download fire angular ui-router otherwise rule?

My routing scheme has an otherwise rule: 我的路由方案有一条其他规则:

$urlRouterProvider.otherwise("/a");

I have this function: 我有这个功能:

function getCSV(TableName) {
            window.location = API_ENDPOINT + '/EditValidationXREF/GetCSV?TableName=' + TableName;
        }

The issue is that when I fire the function getCSV , i may be at state z but I automatically get rerouted to a (which I assume is the otherwise rule in action). 问题是,当我启动函数getCSV ,我可能处于状态z,但我会自动重新路由到a(我认为这是另外一条getCSV的规则)。 Why does this happen and how can I avoid this (ie stay at state z after the download). 为什么会发生这种情况以及如何避免这种情况(即下载后保持在状态z)。

Figured this out. 想通了。 The link that was executing getCSV looked like: 执行getCSV的链接如下所示:

<a href="#" ng-click="vm.getCSV('TableName')">Interchanges</a>

which obviously redirected the browser to the page without the route (and so triggered the otherwise rule as intended). 这显然将浏览器重定向到了没有路由的页面(并因此触发了预期的其他规则)。

Solution: 解:

<a href="javascript:void(0)" ng-click="vm.getCSV('TableName')">Interchanges</a>

The function 功能

function getCSV(TableName) {
            window.location = API_ENDPOINT + '/EditValidationXREF/GetCSV?TableName=' + TableName;
        }

is actually changing the location of the window to something, something that the application is unable to identify to, or resolve. 实际上是将窗口的位置更改为应用程序无法识别或解决的位置。 Thats the reason, its navigating to the URL associated to otherwise. 这就是原因,其导航到其他相关的URL。 One of the alternative methods to simply download the file is to change the above function to: 简单下载文件的另一种方法是将上述功能更改为:

function getCSV(TableName) {
            window.open(API_ENDPOINT + '/EditValidationXREF/GetCSV?TableName=' + TableName, _blank);
        }

This would download the file, without the navigation. 这将下载文件,而无需导航。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM