angular 如何知道对包含“ng-view”元素的模板 View 发出请求?
如果我在我的应用程序中导航直接说
http://localhost/Categories/List/accessories
.仍然向“/或索引模板”发出请求,因为它包含呈现所有 View 所需的“ng-view”元素。
当我从索引导航到/Categories/List/accessories 时,没有再次请求索引。
下面是我的基本路由,部分是从 GitHub 上的 Angular MVC“CookBook”示例中复制的
angular.module('myApp', ['myApp.ctrl.list'])
.config(['$routeProvider', '$locationProvider', function ($routeProvider) {
$routeProvider.when('/', {
templateUrl: '/Home/Splash',
});
$routeProvider.when('/Categories/List/:slug', {
templateUrl: '/Categories/List',
controller: 'listCtrl',
});
$routeProvider.otherwise({
redirectTo: '/'
});
}]);
最佳答案
根据您的路由配置,我相信您的示例 url 缺少井号 (#),因此它应该如下所示:
http://localhost/#/Categories/List/accessories
#号之后的部分是AngularJS要解析的路径(即你定义的路由配置)
当您在浏览器的地址栏中输入上述网址时,浏览器会自动在localhost
上查找index.html
。大多数浏览器(如果不是全部)都设置为在 URL 中仅存在域名时查找 index.html。
#号后面的部分呢?你可能会问。根据定义,# 符号后的路径不会发送到服务器,因此浏览器可能不太关心该部分由什么组成。
在您提到的第二种情况下,AngularJS 不会请求 index.html
,因为它在第一次加载时已被缓存,并且使用了缓存的副本。
关于javascript - Angular 路由和 ng-view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16039348/