简体   繁体   English

角路由在不同的浏览器中表现得奇怪/不同

[英]Angular routing behaving strange/differently with different browsers

I just finished coding a new web project using AngularJS and Bootstrap. 我刚刚使用AngularJS和Bootstrap编码了一个新的Web项目。 The development took place using Brackets, an editing tool that launches Chrome for testing while functioning as the web server. 开发过程是使用Brackets进行的,Brackets是一种编辑工具,可启动Chrome浏览器进行测试,同时充当网络服务器。

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, and this as long as the browser being used is Chrome and the machine is a Windows 10 computer . 到目前为止,无论是将Brackets用作服务器,还是将整个项目部署在Tomcat安装中,并且所使用的浏览器是Chrome且计算机是Windows 10计算机 ,一切都可以按要求进行。

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 (and perhaps central) issue is coming from the way I implemented and use Angular's routing services (or, at least, this is what is suggested by several posts I found). 似乎第一个(也许是核心)问题来自于我实现和使用Angular的路由服务的方式(或者至少,这是我发现的几篇文章所建议的)。 Two things are happening (depending on the browser and the action triggered) pointing in that direction: 朝着该方向发生的两件事(取决于浏览器和触发的动作):

  1. I received many times the error infdig , meaning that there is an infinite reload loop somewhere, 我收到了很多次错误infdig ,这意味着某个地方存在无限的重载循环,

  2. When the user successfully logs into the the system, an object containing the user's details is stored as a $rootScope object, and when a $window.location.href command is used to move to other page, all the user information previously stored disapears (strangely, this s not happening with Chrome, but it is with IE and Edge!). 当用户成功登录系统时,包含用户详细信息的对象将存储为$rootScope对象,并且当使用$window.location.href命令移动到其他页面时,先前存储的所有用户信息都会消失(奇怪的是,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
      index.js              -- Contails the controller related ot the index.html page
      [pages]               -- Sub-folder hosting all the pages of the project except for the `index.html`
         page1.html
         page2.html
         :
         :
      [js]                  -- Sub-folder hosting the controllers of each and every sub-page
         page1.js           -- Sub-page controller
         page2.js           -- Sub-page controller
         :
         :

Transition to the sub-pages (eg page1.html, etc.) takes place using the command $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'
    }
    :
    :

Based on some posts related to how to define routing, 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). (包括此选项时,出现注射错误)。

How can I properly use this Angular routing service, and how can I set HTML5 mode? 如何正确使用此Angular路由服务,如何设置HTML5模式?

Regarding the page not found issue make sure that templateUrl: 'pages/page2.html' has the same path as in the actual folders 关于找不到页面的问题,请确保templateUrl:'pages / page2.html'与实际文件夹中的路径相同

- capital letters  
- the s in "pages" is also present in the pages folder

Also make sure that the permission are ok such that your application is not getting denied access to the file. 还要确保许可权确定,以使您的应用程序不会被拒绝访问文件。 I don't know what OS you are using but make sure your application can access it 我不知道您使用的是什么操作系统,但请确保您的应用程序可以访问它

Regarding the loop error, to help I would need to see a bit more code, but if you open the application in Chrome and see the error in the developer tools it may give you a hint as of where your application is crashing. 关于循环错误,为了帮助我需要查看更多代码,但是如果您在Chrome中打开该应用程序并在开发人员工具中看到该错误,则可能会提示您有关应用程序崩溃的位置。 The other approach is to start commenting part of the application until you don't get the error to find the problematic line then analyze. 另一种方法是开始注释应用程序的一部分,直到您没有错误找到有问题的行然后进行分析。

This is an example of a controller I use without problems: 这是我使用的没有问题的控制器的示例:

在此处输入图片说明

Routing params: the way I've done it and it works for me and its simple is using the same route function I showed before: 路由参数:我完成它的方式对我有用,它的简单方法是使用之前显示的相同路由功能:

在此处输入图片说明

Then if you look at 'searchresult/:searchCriteria' :search criteria is already a parameter that I am putting in a JavaScript variable called sys (ie at the beginning of my JavaScript I declare variable var sys = null; ). 然后,如果您查看'searchresult /:searchCriteria':搜索条件已经是我要放入称为sys的JavaScript变量的参数(即,在我的JavaScript开头,我声明了变量var sys = null; )。

Then on the SearchResult Controller I put the value of sys inside a $scope variable let's say $scope.sys = sys; 然后在SearchResult Controller上,将sys的值放在$ scope变量内,假设$scope.sys = sys; . This gives you the variable both in the scope and in JavaScript if you want to check the values in developer tools console and/or play with them. 如果要检查开发人员工具控制台中的值和/或使用它们,可以在范围和JavaScript中为您提供变量。

To call the pafe http://url/#searchresult/myvalue 调用pafe http:// url /#searchresult / myvalue

Just like before call $location.path("/searchresult/myvalue") 就像调用$ location.path(“ / searchresult / myvalue”)之前一样

like this you can create a path with many arguments (ie "/searchresult/myvalue1/myvalue2/myvalue3") and they all will be stored in the variable sys 像这样,您可以创建一个包含许多参数的路径(即“ / searchresult / myvalue1 / myvalue2 / myvalue3”),它们都将存储在变量sys中

PS: if you want to change your whole url use window.location.replace('new url') without any $ . PS:如果您想更改整个URL,请使用window.location.replace('new url')而不使用任何$ The difference between this routing and the Angular is that this will refresh the page while angular will only refresh your ng-view 此路由和Angular之间的区别在于,这将刷新页面,而angular仅刷新您的ng-view

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

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