角度路由& ng-view

时间:2013-04-16 14:09:10

标签: javascript asp.net-mvc angularjs ngroute

如何知道角度对包含'ng-view'元素的模板视图的请求?

如果我在我的应用程序中导航,请直接说

http://localhost/Categories/List/accessories

。仍然会向'/或索引模板'发出请求,因为它包含呈现所有视图所需的'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: '/'
        });
    }]);

2 个答案:

答案 0 :(得分:3)

使用您的路由配置,我相信您的示例网址缺少英镑符号(#),因此它应该如下所示:

http://localhost/#/Categories/List/accessories

#符号后面的部分是AngularJS要解析的路径(即您定义的路由配置)

当您在浏览器的地址栏中输入上述网址时,浏览器会自动在index.html上查找localhost。大多数浏览器(如果不是全部)都设置为在URL中仅存在域名时查找index.html。

#符号之后的部分怎么样?你可能会问。根据定义,#符号后面的路径不会被发送到服务器,因此浏览器可以更少关注该部分的组成部分。

在你提到的第二个场景中,AngularJS没有请求index.html,因为它在第一次加载时被缓存,并且使用了缓存副本。

答案 1 :(得分:0)

您的locationProvider是否已为HTML5配置?

$locationProvider.html5Mode(true);

请参阅Using HTML5 pushstate on angular.js