简体   繁体   English

Angular ngRoute不适用于Express 4

[英]Angular ngRoute don't work with Express 4

I try set up client side route with Angular and Express 4. I worked with guide ngView and without Express that's good, but when Express routing is enable ngRoute don't work. 我尝试使用Angular和Express 4设置客户端路由。我使用ngView指南,但没有Express的情况很好,但是启用Express路由时,ngRoute不起作用。 How can I setup Express that it work with ngRoute? 如何设置Express与ngRoute一起使用?

A little bit of code: 一点代码:

 var app = angular.module('app', ['ngRoute']); app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { $routeProvider .when('/category/:catgegoryName', { templateUrl: 'category', controller: 'categoryController', controllerAs: 'category' }) .when('/category/:catgegoryName/device/:deviceName', { templateUrl: 'device', controller: 'deviceController', controllerAs: 'device' }) .otherwise({ redirectTo: '/' }); $locationProvider.html5Mode(true); } ]); 

Since you configured HTML5 mode you don't need to replicated the same route structure on the Express side. 由于配置了HTML5模式,因此无需在Express端复制相同的路由结构。 Instead you need to make sure server always responds with landing page (usually index.html ) where Angular app is configured and bootstraped. 相反,您需要确保服务器始终以登录页面(通常为index.html )响应,在该页面中已配置并引导了Angular应用。

So it will be that for say route /category/something server still responds with index.html . 因此,可以说route /category/something服务器仍然使用index.html响应。 Then Angular will parse URL and understand that it need to inject template and controller corresponding to .when('/category/:catgegoryName', {...}) route. 然后,Angular将解析URL并了解它需要注入与.when('/category/:catgegoryName', {...})路由相对应的模板和控制器。

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

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