简体   繁体   English

不同浏览器的 AngularJS 路由服务问题

[英]AngularJS routing services issues with different browsers

I just finished coding a new WEB project using AngularJS and Bootstrap.我刚刚使用 AngularJS 和 Bootstrap 完成了一个新的 WEB 项目的编码。 The development took place using Brackets, which launches Chrome for testing (while Brackets itself functions as the SERVER).开发是使用 Brackets 进行的,它启动 Chrome 进行测试(而 Brackets 本身充当服务器)。

So far, everything works as required both when Brackets is used as the SERVER as well as when the whole project is deployed within a TOMCAT installation as long as the browser being used is Chrome and the machine is my computer .到目前为止,只要使用的浏览器是 Chrome 并且机器是我的电脑,无论是使用 Brackets 作为 SERVER 还是在 TOMCAT 安装中部署整个项目时,一切都按要求工作。

Now, I started testing the project using different browsers and devices (eg tablets, mobiles, etc.) and... OOPS!!!现在,我开始使用不同的浏览器和设备(例如平板电脑、手机等)测试该项目,并且……哎呀!!! I am getting crashes all the time.我总是崩溃。

It would appear that the first issue is coming from the way I implemented and use the routing services (or, at least, this is what is suggested from several posts I found).第一个问题似乎来自我实现和使用路由服务的方式(或者,至少,这是我发现的几篇文章所建议的)。 Two things are happening (depending on the browser and the action triggered):发生了两件事(取决于浏览器和触发的操作):

  1. I received many times the error infdig ,我多次收到错误infdig

  2. When the user logs into the the system and a $window.location.href command is used to move to other page, all the user information stored within a $rootScope object disapears (strangely, this s not happening with Chrome, but it is with IE and Edge!).当用户登录系统并使用$window.location.href命令移动到其他页面时,存储在$rootScope对象中的所有用户信息都消失了(奇怪的是,Chrome 没有发生这种情况,但它是IE 和 Edge!)。

Unfortunately, I was unable to fully understand what is the proper way of using the routing services.不幸的是,我无法完全理解使用路由服务的正确方法是什么。

The structure of the project is:该项目的结构是:

 [MyApp]                    -- This is the folder containing the whole project under TOMCAT's "webapps" folder
      index.html
      [pages]               -- Sub-folder hosting all the pages of the project except for the `index.html`
         page1.html
         page2.html
         :
         :

Transition to the sub-pages (page1.html, etc.) takes place using the $window.location.href = "#page1.html";使用$window.location.href = "#page1.html";转换到子页面(page1.html 等) $window.location.href = "#page1.html"; , and the routing service is defined: ,并且路由服务定义为:

$routeProvider
    .when('page1.html', {
            templateUrl: '#/pages/page1.html',
            controller: 'Page1Controller'
    }
    .when('page2.html', {
            templateUrl: '#/pages/page2.html',
            controller: 'Page2Controller'
    }
    :
    :

From posts on this, I also tried:从关于这个的帖子中,我也尝试过:

    .when('page1.html', {
            templateUrl: 'pages/page1.html',
            controller: 'Page1Controller'
    }

and

    .when('page1.html', {
            templateUrl: '/pages/page1.html',
            controller: 'Page1Controller'
    }

getting errors in both cases (eg page not found).在这两种情况下都会出错(例如找不到页面)。

Additionally, it is not clear to me what is the impact of including the statement $locationProvider.html5Mode(true);此外,我不清楚包含语句$locationProvider.html5Mode(true);的影响是什么$locationProvider.html5Mode(true); (when including this, I got an injection error). (当包括这个时,我得到了一个injection错误)。

I am seeking a simple and straightforward explanation on how to properly use this service, and if and how to set HTML5 mode.我正在寻求有关如何正确使用此服务以及是否以及如何设置 HTML5 模式的简单明了的解释。

After some struggle, I was able to fix things.经过一番挣扎,我能够解决问题。 Here is a summary of the issues and their solutions for those that might encounter the same situations.以下是针对可能遇到相同情况的人的问题及其解决方案的摘要。

  • Different filename case in different OSs: As presented in the body of the original question, the development took place using Brackets (Great tool!!!) running on Windows.不同操作系统中的不同文件名大小写:如原始问题的正文所示,开发是使用在 Windows 上运行的 Brackets(很棒的工具!!!)进行的。 As everybody knows, windows is case-insensitive as far as filenames is concerned.众所周知,就文件名而言,windows 是不区分大小写的。 As such, if you have a page named abc.html and you invoke it as Abc.html , the page WILL be found.因此,如果您有一个名为abc.html的页面并且您将其作为Abc.html调用, Abc.html找到该页面。 This is not the case in Linux and hence, once I reviewed all the references and synchronized all the names, this pare of the issue was resolved.这在 Linux 中并非如此,因此,一旦我查看了所有引用并同步了所有名称,这个问题就解决了。

  • Correct usage of `$location' service: In my code there were many inconsistencies in the way I was using this service. `$location' 服务的正确用法:在我的代码中,我使用这个服务的方式有很多不一致之处。

The correct (at least for me) way of using the routing services is as follows:使用路由服务的正确(至少对我而言)方式如下:

  • In the Route Provider section, make sure that you have entries for all the pages you are directing to, like:在 Route Provider 部分,确保您有指向的所有页面的条目,例如:

    .when ('/Demos', { templateUrl: 'pages/Demos.html' , controller: 'DemosController' }) .when ('/Demos', { templateUrl: 'pages/Demos.html' , 控制器: 'DemosController' })

  • Then, when redirecting, use the syntax:然后,在重定向时,使用以下语法:

    $location.path('/Demos') ; $location.path('/Demos') ;

Once these issues were resolved, my application was tested with Firefox, IE, Edge, Chrome and Safari and, except for differences in the way the browser presents certain fields (like datetime picker), the behavior in all is identical (if we ignore performance differences when using IE).一旦这些问题得到解决,我的应用程序就用 Firefox、IE、Edge、Chrome 和 Safari 进行了测试,除了浏览器呈现某些字段的方式不同(如日期时间选择器)外,所有行为都是相同的(如果我们忽略性能使用 IE 时的差异)。

Hope this is of help for the needed.希望这对需要的人有所帮助。

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

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